【发布时间】:2011-06-13 17:35:53
【问题描述】:
我尝试使用资源中的图像。
- 我将一张 png 图片添加到资源中,名称为 heart.png。这是公共财产。
- 我在这里公开资源与类作为建议: http://houseofbilz.com/archives/2009/03/15/binding-to-resources-in-silverlightwpf/
这是类:
命名空间 Spirit.Util { 使用属性;
public class PublicResources
{
private readonly static Resources Resources = new Resources();
public Resources SpiritResources { get { return Resources; } }
}
}
我添加到 app.xaml:
<Util:PublicResources x:Key="SpiritResources"/>
并尝试在图像控制上使用。
<Image Style="{StaticResource InfoIcon}">
<Image.Source>
<!--<MultiBinding Converter="{StaticResource imageToGrayConverter}">-->
<Binding Path="SpiritResources.heart" Source="{StaticResource SpiritResources}"/>
<!--<Binding Path="Oponent.Info.IsFriend" Mode="OneWay" UpdateSourceTrigger="PropertyChanged"/>
</MultiBinding>-->
</Image.Source>
</Image>
第一个问题是图像控件是空的,为什么?
我的完整目标是使用多绑定和多转换器从图像控制资源中绑定图像。 如果属性 Isfriend (Oponent.Info.IsFriend) 为 false,我想将图像转换为灰度。
另一个问题来了。我使用这个转换器类将图像转换为灰度。
public class ImageToGrayConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
//string imageUri = values[0] as BimapImage;
//value is type of System.Drawing.Image
var image = values[0] as BitmapImage; //new BitmapImage(new Uri(imageUri, UriKind.Relative));
string s = values[1].ToString();
bool isLogged = System.Convert.ToBoolean(s);
if (!isLogged)
{
try
{
if (image != null)
{
var grayBitmapSource = new FormatConvertedBitmap();
grayBitmapSource.BeginInit();
grayBitmapSource.Source = image;
grayBitmapSource.DestinationFormat = PixelFormats.Gray32Float;
grayBitmapSource.EndInit();
return grayBitmapSource;
}
return null;
}
catch (Exception ex)
{
throw ex;
}
}
return image;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
Value 是 System.Drawing.BitmapImage 的类型,我想我可以用这个简单的类将 Bitmap 转换为 BitmapImage 类:
http://dog-net.org/content/development/wpf/system-drawing-bitmap-to-bitmapimage/
但我必须首先解决第一个问题。感谢您的建议。
【问题讨论】:
标签: wpf image binding resources converter