我为一个复选框制作了一个自定义图像,并希望它在每次单击图像时在选中版本和未选中版本之间切换。我的源代码:
XAML
<CheckBox Name="checkBox1" Padding="0" BorderThickness="0" Margin="5" Grid.Row="1">
<Image Name="image1" Margin="-14,0,0,0" Source="checkbox0.png" MouseDown="Image_MouseDown" Stretch="Uniform"/>
</CheckBox>
C#
private void Image_MouseDown(object sender, MouseButtonEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed)
{
if (checkBox1checked)
{
image1.BeginInit();
image1.Source = new BitmapImage(new Uri("/checkbox0.png", UriKind.RelativeOrAbsolute));
image1.EndInit();
checkBox1checked = false;
}
if (!checkBox1checked)
{
image1.BeginInit();
image1.Source = new BitmapImage(new Uri("/checkbox1.png", UriKind.RelativeOrAbsolute));
image1.EndInit();
checkBox1checked = true;
}
}
}