图像UriSource和数据绑定我正在尝试将自定义对象列表绑定到WPF图像,如下所示:<Image>
<Image.Source>
<BitmapImage UriSource="{Binding Path=ImagePath}" />
</Image.Source></Image>但它不起作用。这是我得到的错误:必须设置“Property'UriSource'或属性'StreamSource'。”我错过了什么?
3 回答
侃侃尔雅
TA贡献1801条经验 获得超16个赞
您也可以简单地设置Source属性而不是使用子元素。为此,您的类需要将图像作为位图图像返回。这是我做过的一种方式的例子
<Image Width="90" Height="90"
Source="{Binding Path=ImageSource}"
Margin="0,0,0,5" />而class属性就是这个
public object ImageSource {
get {
BitmapImage image = new BitmapImage();
try {
image.BeginInit();
image.CacheOption = BitmapCacheOption.OnLoad;
image.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
image.UriSource = new Uri( FullPath, UriKind.Absolute );
image.EndInit();
}
catch{
return DependencyProperty.UnsetValue;
}
return image;
}
}我想它可能比价值转换器多一点工作,但它是另一种选择。
- 3 回答
- 0 关注
- 1166 浏览
添加回答
举报
0/150
提交
取消
