在运行时更改图像源不会显示图像
我试图根据其绑定值在 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 接口将状态转换为将在模板中显示的图像,这里是 Converter 类,
[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 接口,因此接口知道属性何时自动更改,所以我立即知道这不是问题:)
我已经通过谷歌大学进行了搜索,并看到很多原则上有相同问题的帖子但在使用转换器接口和项目资源时没有遇到解决方案。
有人可以帮忙吗?提前致谢
我的所有其他 IValueConverter 类都运行良好,但这个除外。
I am trying to display an Image within a ListBox.ItemTemplate depending on its binding value, the binding value is the status of an object (pending, retrieved, posted, complete or errored), here is the XAML for the Image element.
<Window.Resources>
<local:StatusImageConverter x:Key="StatusImage" />
</Window.Resources>
<Image Source="{Binding Path=Status, Converter={StaticResource StatusImage}}" />
I have added 2 images (Badge_tick, Badge_cross) to the Project resource and use the IValueConverter interface to convert the status to an Image which will be displayed in the template, here is the Converter class
[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
}
}
This builds/compiles fine and runs but when the status changes the image is not displayed within the TemplateItem. I am using the INotifyPropertyChanged interface within my classes so the interface knows when the property is changed automatically so I know straight away that is not the problem :)
I have trawled through the university of google and seen lot of posts with the same problem in principle but not come accross a solution when using the converter interface and project resources.
Can anyone help? Thanks in advance
All my other IValueConverter classes are working perfectly, just not this one.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试返回 BitmapSource 类型来代替
要更改的 Bitmap 位:
并返回 BitmapImage,如下所示:
Try returning a BitmapSource type inplace of Bitmap
Bits to change:
and return a BitmapImage as in:
我怀疑问题可能在于您使用标准 Bitmap 类,该类不是 ImageSource 派生类型。
您需要使用 ImageSource 类型:
http://msdn.microsoft.com/en -us/library/system.windows.media.imaging.bitmapimage.aspx
如果您不了解包 URI,请参阅此内容:
http://msdn.microsoft.com/en-us/library/aa970069.aspx
I suspect the problem may be in your use of the standard Bitmap class, which is not an ImageSource derived type.
You need to use an ImageSource type:
http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.bitmapimage.aspx
See this if you don't know about pack URIs:
http://msdn.microsoft.com/en-us/library/aa970069.aspx