在设计时使用Value Conveter进行数据绑定的图像源问题 [英] Image Source with Data Binding using Value Conveter at Design Time issue
问题描述
我创建了一个简单的页面,并使用XAML文件添加了一些设计数据。
在我的页面中,我有一个带有ItemTemplate的ListBox,它使用转换器将Image元素绑定到我模型中的属性:
< 图像 来源 =" { 绑定 转换器 = { StaticResource CategoryToIconValueConverter }, 路径 = 类别 } " />
转换器定义如下:
public class CategoryToIconValueConverter : < span style ="color:#2b91af"> IValueConverter
{
public object 转换( object value, 输入 targetType, object 参数,
System.Globalization。 CultureInfo culture)
; {
string category = (字符串跨度>)值;
&NBSP;&NBSP;&NBSP;&NBS p; return string .Format("图片/ {0} .png" , 类别); }
; public object ConvertBack( object value, 输入 targetType, object 参数, ; System.Globalization。 CultureInfo culture) {
throw new NotImplementedException ();
}
}
出于某种原因,我无法看到de中的图像标志视图,而在运行时它很好。我在同一数据模板中使用的其他转换器在设计时工作。
我也试过实现另一个转换器,但同样的问题也出现了。
public class CategoryToImageSourceValueConverter : IValueConverter
{
public object 转换(对象 值, 类型 targetType, 对象 参数,
System.Globalization。 CultureInfo culture)
{
字符串 类别 = ( string )值;
string categoryImage = string .Format(" Images / {0} .png" , category); try
{
return new BitmapImage (新 Uri (categoryImage, UriKind .Relative));
}
catch
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; {
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;return DependencyProperty .UnsetValue;
;} }
public object ConvertBack( object value, 类型 targetType, object 参数,
System.Globalization。 CultureInfo culture)
{
throw new NotImplementedException ();
}
}
有什么想法?
谢谢,
Guy
Guy Burstein
你不应该使用在设计时可能失败的应用程序相关资源引用,使用基于组件的语法
string category = ( string ) value ;
string categoryImage = string 。 格式 ( " / AppHostName; component / Images / {0} .png" , category );感谢跨度>
Hi,
I created a simple page and added some design data using a XAML file.
In my page, I have a ListBox with ItemTemplate that has an Image element bound to a property in my model using a converter:
<Image Source="{Binding Converter={StaticResource CategoryToIconValueConverter}, Path=Category}" />
The converter is defined like this:
public class CategoryToIconValueConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
string category = (string)value;
return string.Format("Images/{0}.png", category);
}
public object ConvertBack(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
For some reason, I cannot see the images in design view, while in run time it works great. Other converters I have used in the same data template work in design time.
I also tried implementing another converter, but the same issue occurs.
public class CategoryToImageSourceValueConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
string category = (string)value;
string categoryImage = string.Format("Images/{0}.png", category);
try
{
return new BitmapImage(new Uri(categoryImage, UriKind.Relative));
}
catch
{
return DependencyProperty.UnsetValue;
}
}
public object ConvertBack(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
Any idea?
Thanks,
Guy
Guy Burstein
Hi,
You should not use application-relative resource references which may fail at design time, use component-based syntax
string category = (string)value; string categoryImage = string.Format("/AppHostName;component/Images/{0}.png", category);
Thanks
这篇关于在设计时使用Value Conveter进行数据绑定的图像源问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!