C# 更改图像不能与WPF一起动态工作?
我想在按钮上动态更改图像。我想我有找到图像所需要的东西。我有som resourcedictionary用于这里的按钮:C# 更改图像不能与WPF一起动态工作?,c#,.net,wpf,wpf-controls,caliburn.micro,C#,.net,Wpf,Wpf Controls,Caliburn.micro,我想在按钮上动态更改图像。我想我有找到图像所需要的东西。我有som resourcedictionary用于这里的按钮: <BitmapImage x:Key="Mark1Empty" UriSource="Marks/Tom1.png"/> <BitmapImage x:Key="Mark1Chosen" UriSource="Marks/Ny1.png"/> 检查一下这个方法:为什么不直接看到image.Source=imagePicker;?为什么要尝试创造一个全
<BitmapImage x:Key="Mark1Empty" UriSource="Marks/Tom1.png"/>
<BitmapImage x:Key="Mark1Chosen" UriSource="Marks/Ny1.png"/>
检查一下这个方法:为什么不直接看到image.Source=imagePicker;?为什么要尝试创造一个全新的形象?您确定资源URI相对于程序运行时的位置仍然有效吗?
<Button x:Name="Mark1Button"
Height="30" Width="40" Template="{StaticResource Mark1ButtonTemplate}"
cal:Message.Attach="[Event Click] = [Action SayHello($source, $this)]"/>
<ControlTemplate x:Key="Mark1ButtonTemplate" TargetType="{x:Type Button}">
<Image Name="Mark1ButtonImage" Source="{StaticResource Mark1Empty}" />
</ControlTemplate>
public void SayHello(object sender, object kravsource)
{
var selectedButton = sender as Button;
if (selectedButton != null)
{
Image image = (Image)selectedButton.Template.FindName("Mark1ButtonImage", selectedButton);
BitmapImage imagePicker = (BitmapImage)Application.Current.FindResource("Mark1Chosen");
image.Source = new BitmapImage(new Uri(imagePicker.UriSource.ToString(), UriKind.RelativeOrAbsolute));
image.Stretch = Stretch.Uniform;
}
}