C# 自定义按钮,单击后更改图像
嗨,我有那种按钮: XAML: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
当我点击按钮图像消失时,我尝试了其他几种方法,但我做不到。我搜索了这个论坛,但没有找到任何能真正帮助我的东西。您还没有设置
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;
}