点击更改图像源

时间:2012-06-28 12:26:54

标签: c# wpf

我为复选框创建了一个自定义图像,并希望每次单击图像时它都在已检查和未检查的版本之间切换。我的源代码:

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;
         }
     }
}

3 个答案:

答案 0 :(得分:3)

使用复选框的取消选中和检查事件,并在那里设置图像源

<CheckBox Name="checkBox1" Padding="0" Unchecked="chkbox_Unchecked" Checked="chkbox_Checked BorderThickness="0" Margin="5" Grid.Row="1">
   <Image Name="image1" Margin="-14,0,0,0" Source="checkbox0.png" Stretch="Uniform"/>
</CheckBox>




        private void chkbox_Checked(object sender, RoutedEventArgs e)
        {
            SetImage("/checkbox0.png");
        }

        private void chkbox_Checked(object sender, RoutedEventArgs e)
        {
             SetImage("/checkbox1.png");
        }

    private void SetImage(string path)
    {
        image1.BeginInit();
                image1.Source = new BitmapImage(new Uri(path, UriKind.RelativeOrAbsolute));
                image1.EndInit();

    }

答案 1 :(得分:1)

因为你的代码背后存在逻辑错误,所以第二个if语句将在第一个之后执行,因为你更改了复选框的值。

你也在复选框中定义了图像,每次初始化图像时都会以某种方式重置复选框的值,考虑在复选框之外移动图像或只使用公共标记

<CheckBox Name="checkBox1" Padding="0" BorderThickness="0" Margin="5" Grid.Row="1">
</CheckBox>
<Image Name="image1" Margin="-14,0,0,0" Source="checkbox0.png" MouseDown="Image_MouseDown" Stretch="Uniform"/>

改变你的代码

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;
            }
            else
            {
                image1.BeginInit();
                image1.Source = new BitmapImage(new Uri("/checkbox1.png", UriKind.RelativeOrAbsolute));
                image1.EndInit();
                checkBox1checked = true;
            }
        }
    }

答案 2 :(得分:0)

更好的尝试:

    private void chkbox_Unchecked(object sender, RoutedEventArgs e)
    {
         SetImage("/checkbox1.png");
    }

        private void chkbox_Checked(object sender, RoutedEventArgs e)
    {
         SetImage("/checkbox1.png");
    }