WPF -更改按钮图像源时,单击按钮
本文关键字:按钮 单击 图像 WPF | 更新日期: 2023-09-27 17:49:18
当发生单击事件时,我试图更改图像源。我可以在XAML中更改图像,但不能从代码中更改。我试过从按钮获取图像属性,但这不起作用。
我正在使用Caliburn事件调用:
<Button Content="Mark1" Height="30" Width="40"
cal:Message.Attach="[Event Click] = [Action SayHello($source, $this)]">
<Image Name="Mark1ButtonImage" Source="image1.png" />
</Button>
在c#代码中:
public void SayHello(object sender, object doodlesource)
{
var selectedButton = sender as Button;
var selectedKrav = doodlesource as Doodle;
if (selectedButton != null)
{
///WHAT TO DO TO CHANGE? selectedButton.Image doesn't work?
}
}
试试这个
Image img = new Image();
img.Source = new BitmapImage(new Uri(@"foo.png"));
SelectedBtn.Content=img
要在按钮内找到您的图像,您需要使用这行代码:
var image = (sender as Button).FindVisualChildren<Image>();
然后你可以改变图片的来源