[英]Binding a wpf xaml image to System.Windows.Controls.Image is not working
I have an image in wpf application and bind its Source property to System.Windows.Controls.Image in view model but it is not working.我在 wpf 应用程序中有一个图像,并将其 Source 属性绑定到 System.Windows.Controls.Image 在视图 model 中,但它不起作用。 But when i bind its Source property to a BitmapImage, it is working.但是当我将其 Source 属性绑定到 BitmapImage 时,它正在工作。 Is there any way to bind Source property as System.Windows.Controls.Image?有没有办法将 Source 属性绑定为 System.Windows.Controls.Image?
Xaml Part Xaml 零件
<Image x:Name="ShipMainImage" Source="{Binding MainImageSource}" />
View model Part查看 model 零件
public class StabilityVM : INotifyPropertyChanged {
private System.Windows.Controls.Image mainImageSource;
public System.Windows.Controls.Image MainImageSource
{
get { return mainImageSource; }
set {
mainImageSource = value;
OnPropertyChanged(nameof(MainImageSource)); }
}
protected virtual void OnPropertyChanged(string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public event PropertyChangedEventHandler PropertyChanged;
} }
Do not use System.Windows.Controls.Image
as type of that property.不要使用System.Windows.Controls.Image
作为该属性的类型。 It is a UI element type that is used in a view, but not in a view model.它是在视图中使用的 UI 元素类型,但不在视图 model 中使用。
Declare the property as System.Windows.Media.ImageSource
- the type of the Source
property of the Image element:将属性声明为System.Windows.Media.ImageSource
- Image 元素的Source
属性的类型:
private ImageSource mainImageSource;
public ImageSource MainImageSource
{
get { return mainImageSource; }
set
{
mainImageSource = value;
OnPropertyChanged(nameof(MainImageSource));
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.