Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 自定义按钮,单击后更改图像_C#_Wpf_Button_Mouseclick Event - Fatal编程技术网

C# 自定义按钮,单击后更改图像

C# 自定义按钮,单击后更改图像,c#,wpf,button,mouseclick-event,C#,Wpf,Button,Mouseclick Event,嗨,我有那种按钮: XAML: 当我点击按钮图像消失时,我尝试了其他几种方法,但我做不到。我搜索了这个论坛,但没有找到任何能真正帮助我的东西。您还没有设置ContentPresenterContent属性。您需要使用UriKind.Absolute作为图像路径 BitmapImage bmi = new BitmapImage(new Uri("C:\\Users\\Daniel\\Desktop\\WPF\\Paint\\skew2.png", UriKind.A

嗨,我有那种按钮:

XAML:


当我点击按钮图像消失时,我尝试了其他几种方法,但我做不到。我搜索了这个论坛,但没有找到任何能真正帮助我的东西。

您还没有设置
ContentPresenter
Content
属性。

您需要使用
UriKind.Absolute
作为图像路径

  BitmapImage bmi = 
      new BitmapImage(new Uri("C:\\Users\\Daniel\\Desktop\\WPF\\Paint\\skew2.png",
      UriKind.Absolute));
  bbb.Source = bmi ;
XAML

或C#


或者根本就不要设置任何类型。
private void Button_Click(object sender, RoutedEventArgs e)
{
    BitmapImage bmi = new BitmapImage(new Uri("C:\\Users\\Daniel\\Desktop\\WPF\\Paint\\skew2.png", UriKind.Relative));
    bbb.Source = bmi ;
}
  BitmapImage bmi = 
      new BitmapImage(new Uri("C:\\Users\\Daniel\\Desktop\\WPF\\Paint\\skew2.png",
      UriKind.Absolute));
  bbb.Source = bmi ;
        <Button x:Name="button" Content="Button1" HorizontalAlignment="Left" Margin="400,20,0,0" VerticalAlignment="Top" RenderTransformOrigin="-1.258,-5" Click="Button_Click" Height="80" Width="80"/>
    private void Button1_Click(object sender, RoutedEventArgs e)
    {
        button1.Background = new ImageBrush { ImageSource = new BitmapImage(new Uri("ms-appx:/Images/timerg.png", UriKind.RelativeOrAbsolute)) };

    }
    private void Button1_Click(object sender, RoutedEventArgs e)
    {
            BitmapImage bmp = new BitmapImage();
            Uri u = new Uri("ms-appx:/Images/timer.png", UriKind.RelativeOrAbsolute);
            bmp.UriSource = u;
            // NOTE: change starts here
            Image i = new Image();
            i.Source = bmp;
            button1.Content = i;
    }