我在使用 IValueconverter 时遇到问题并动态加载行网格图像:
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
string Type = (string)value;
Uri uri;
try
{
uri = new Uri("/XConsole;component/Images/" + Type + ".png", UriKind.Relative);
return new System.Windows.Media.Imaging.BitmapImage(uri) { CacheOption = BitmapCacheOption.None };
}
catch (Exception e)
{
//donothing
}
uri = new Uri("/XConsole;component/Images/Type_SYSTEM.png", UriKind.Relative);
return new System.Windows.Media.Imaging.BitmapImage(uri) { CacheOption = BitmapCacheOption.None };
}
我想将对象的“类型”传递给转换器并加载不同的图像:
<Image Grid.Column="0" Width="25" Height="25" Margin="2,0" Source="{Binding Path=Type, Converter={StaticResource imageConverter}}" >
但是有问题,因为没有加载图片!
如果我静态加载,没关系:
<Image Grid.Column="0" Width="25" Height="25" Margin="2,0" Source="/XconsoleTest;component/Images/Type_DB.png" >
我在 UserControl.Resources 中加载了我的转换器:
<UserControl.Resources>
<converters:ImageConverter x:Key="imageConverter"/>
</UserControl.Resources>
帮我!!