WPF ImageSource 与自定义转换器绑定
我有一个以这种方式构造的组合框的简单模板:
<ComboBox DockPanel.Dock="Left" MinWidth="100" MaxHeight="24"
ItemsSource="{Binding Actions}">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name}" Width="100" />
<Image Source="{Binding Converter={StaticResource TypeConverter}}" />
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
因此,如果我使用此代码,一切正常:
<TextBlock Text="{Binding Name}" Width="100" />
<!--<Image Source="{Binding Converter={StaticResource TypeConverter}}" /> -->
<Image Source="{StaticResource SecurityImage}" />
但如果我使用转换器,它就不再工作了。 这是转换器,但我不知道如何从那里引用静态资源......
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var type = (Action)value;
var img = new BitmapImage();
switch (type.ActionType)
{
case ActionType.Security:
img.UriSource = new Uri("StructureImage", UriKind.Relative);
break;
case ActionType.Structural:
img.UriSource = new Uri("SecurityImage", UriKind.Relative);
break;
}
return img;
}
I have a simple template for a combobox structured in this way:
<ComboBox DockPanel.Dock="Left" MinWidth="100" MaxHeight="24"
ItemsSource="{Binding Actions}">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name}" Width="100" />
<Image Source="{Binding Converter={StaticResource TypeConverter}}" />
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
So, if I use this code, everything works:
<TextBlock Text="{Binding Name}" Width="100" />
<!--<Image Source="{Binding Converter={StaticResource TypeConverter}}" /> -->
<Image Source="{StaticResource SecurityImage}" />
But if I use the converter it doesn't work anymore.
This is the converter, but I don't know how I can refer to the static resource from there ...
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var type = (Action)value;
var img = new BitmapImage();
switch (type.ActionType)
{
case ActionType.Security:
img.UriSource = new Uri("StructureImage", UriKind.Relative);
break;
case ActionType.Structural:
img.UriSource = new Uri("SecurityImage", UriKind.Relative);
break;
}
return img;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试使用 Josh 编写的 Switch Converter,应该适合您:
SwitchConverter –
无需编写转换器,您的代码将如下所示 -
更新 1:
这是 SwitchConverter 的代码,因为 Josh 的 网站似乎已关闭 -
更新 2:
Try to use the Switch Converter written by Josh, should work for you:
SwitchConverter –
No need to write your converter, your code will look like this -
Update1:
Here is code of SwitchConverter as Josh's site seems to be down -
Update2:
Another SwitchConverter implementation from Microsoft Reference Source.
使用
Image.UriSource
时,如果图像已添加到项目中并且其“构建操作”已设置为“资源”,则需要指定图像的相对文件路径。例如,如果您已将图像放入 Visual Studio 中名为“images”的项目文件夹中,则可以通过以下方式引用图像:如果图像未构建为资源,则必须使用完整文件路径,即
< strong>编辑:
如果将图像放入应用程序资源字典中,则始终可以通过以下方式访问它:
如果将资源放在其他位置,则可以使用
IMultiValueConverter
而不是 < code>IValueConverter 为您的转换器。那么您的类型转换器将类似于以下内容:您的 XAML 将类似于以下内容:
最后,这将是您定义资源的方式:
上面的代码未经测试!
When using
Image.UriSource
you need to specify the relative file path to your images if the images have been added to your project and their "Build Action" has been set to "Resource". E.g. if you have put your images in a project folder in Visual Studio called "images", you can refer to the images in the following way:If the images are not build as a resource, you have to use the full file path i.e.
EDIT:
If you put your images in your Application resourcedictionary, you can always access it in the following way:
If you put the resources somewhere else you may use a
IMultiValueConverter
instead ofIValueConverter
for your converter. Then your typeconverter would look something like the following:and your XAML would look similar to this:
Finally, this would be how you'd define your resources:
The above code is untested!