WPF属性问题ImageSource与BitmapImage [英] WPF property problem ImageSource vs BitmapImage
问题描述
大家好
我有一个usercontrol,它从名为Icon的类属性中接收其图像,该属性目前为ImageSource类型.我想将其更改为bitmapimage,因为我想在代码中更改imagesource,而bitmapimage提供了使用BitmapImage.UriSource进行操作的简便方法.
这是我的UserContol Xaml databind
Hi all
I have usercontrol which receives its image from its class property called Icon which is type ImageSource at the moment. I like to change it to bitmapimage because I want to change imagesource in code and bitmapimage offers easy way to do it with BitmapImage.UriSource.
Here is my UserContol Xaml databind
<Image x:Name="imageButtonIcon" Source="{Binding Icon, ElementName=UserControl}" Stretch="Fill"> </Image>
这是可行的,但不需要依赖项属性
And here is working but not wanted dependency property
/// Defines imagesource on buttonicon
public ImageSource Icon
{
get { return (ImageSource)GetValue(IconProperty); }
set { SetValue(IconProperty, value); }
}
// Using a DependencyProperty as the backing store for Image. This enables animation, styling, binding, etc...
public static readonly DependencyProperty IconProperty = DependencyProperty.Register("Icon", typeof(ImageSource), typeof(MyUserControl), new UIPropertyMetadata(null));
而且,当我仅将上述类型的ImageSource更改为BitmapImage时,它将可以正常编译,但在运行时客户端应用程序将遇到错误"Set属性引发异常"
希望你有主意:)
干杯!
And when I just change above type ImageSource to BitmapImage it will compile fine but at the runtime client applications will face error "Set property threw an exception"
Hope you got idea :)
Cheers!
推荐答案
>!我发现不用改变类型,而是可以使用imagesource本身做同样的事情
Ugh! Instead of changing type I found out that I can do same with imagesource itself
ImageSourceConverter imgConv = new ImageSourceConverter();
string path = "pack://application:,,/Assets/flower.jpg"
ImageSource imageSource = (ImageSource) imgConv.ConvertFromStringpath);
image1.Source = imageSource;
我真的应该在累的时候停止编码,导致出现一些假的问题:)
祝你有美好的一天!
I really should stop coding when tired, leads to dummy questions :)
Have a nice day!
这篇关于WPF属性问题ImageSource与BitmapImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!