我爲複選框製作了自定義圖片,並希望每次點擊圖片時都會在已選中和未選中的版本之間切換。我的源代碼:點擊更改圖片源
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;
}
}
}
什麼是你的問題?它不起作用嗎?你有錯誤嗎?你有什麼嘗試? –