使用IValueConverter动态加载图像源
我有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 }; }
我想传递给我的对象的“类型”转换器并加载不同的图像:
但有一些错误,因为没有加载图片!
如果我静态加载,没关系:
我将我的转换器加载到UserControl.Resources:
帮我!!
如果您在代码中创建URI,则必须编写完整的Pack URI,包括pack://application:,,,
part。 在XAML中,这不是必需的,因为它是由字符串到ImageSource TypeConverter自动添加的。
var type = (string)value; var uri = new Uri("pack://application:,,,/XConsole;component/Images/" + type + ".png");
请注意,上面的代码示例使用小写type
标识符而不是Type
以避免与Type
类混淆。