我在使用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>
帮助我!!