我想显示一个ListBox.ItemTemplate内的图像取决于其绑定值,绑定值是一个对象的状态(挂起,检索,张贴,完成或错误),这里是Image元素的XAML。在运行时更改图像源不显示图像
<Window.Resources>
<local:StatusImageConverter x:Key="StatusImage" />
</Window.Resources>
<Image Source="{Binding Path=Status, Converter={StaticResource StatusImage}}" />
我加入2幅图像(Badge_tick,Badge_cross)到项目的资源和使用的IValueConverter接口状态转换为将显示在模板中的图片,这里是转换器类
[ValueConversion(typeof(PreTripItem.PreTripItemStatus), typeof(Bitmap))]
public class StatusImageConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
PreTripItem.PreTripItemStatus status = (PreTripItem.PreTripItemStatus)value;
switch (status)
{
case PreTripItem.PreTripItemStatus.Complete:
return new Bitmap(Properties.Resources.Badge_tick);
case PreTripItem.PreTripItemStatus.Error:
return new Bitmap(Properties.Resources.Badge_cross);
default:
return null;
}
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException(); //Does not need to be converted back
}
}
这建立/编译罚款和运行,但是当状态改变图像不显示在TemplateItem内。我在我的类中使用INotifyPropertyChanged接口,所以界面知道何时自动更改属性,所以我马上就知道这不是问题:)
我已经浏览了google的大学,并看到很多帖子原则上同样的问题,但是在使用转换器接口和项目资源时不能解决问题。
任何人都可以帮忙吗?在此先感谢
我所有的其他IValueConverter类都运行完美,只是不是这一个。
这工作,谢谢。只有你的答案的缺点,我现在必须学习乌里的大声笑 –