【发布时间】:2013-09-04 22:52:28
【问题描述】:
我在Main.xaml 中有这个ResourceDictionary:
<Window.Resources>
<ResourceDictionary>
<BitmapImage x:Key="Customer" UriSource="Icons/customer.png"/>
<BitmapImage x:Key="Project" UriSource="Icons/project.png"/>
<BitmapImage x:Key="Task" UriSource="Icons/task.png"/>
</ResourceDictionary>
</Window.Resources>
我最初使用以下方法设置图像:
<Image Name="TypeIcon" HorizontalAlignment="Left" VerticalAlignment="Center"
Source="{StaticResource Customer}" Height="16" Width="16"/>
我正在尝试在 C# 方法中将 TypeIcon 的 Source 从 Customer 更改为 Project。
我尝试过使用:
TypeIcon.Source = "{StaticResource Project}";
但我收到此错误:
无法将类型
string隐式转换为System.Windows.Media.ImageSource
我尝试使用new ImageSource() 定义图像,但这也不起作用。
如何在 C# 中以编程方式更改图像的 Source?
【问题讨论】:
标签: c# wpf xaml staticresource