简体   繁体   English

将 wpf xaml 图像绑定到 System.Windows.Controls.Image 不起作用

[英]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.

相关问题 如何在WPF桌面应用程序的XAML中将System.Windows.Controls.Image设置为静态 - How to set System.Windows.Controls.Image as static in XAML in a WPF Desktop Application WPF - 从另一个线程更新“System.Windows.Controls.Image” - WPF - Update “System.Windows.Controls.Image” from another thread 无法在WPF中将System.windows.Controls.Image隐式转换为System.windows.form.control - Can't implicitly convert System.windows.Controls.Image into System.windows.form.control in WPF 将System.Drawing.Image转换为System.Windows.Controls.Image? - Convert System.Drawing.Image to System.Windows.Controls.Image? 访问嵌入的图像并创建System.Windows.Controls.Image - Accessing embedded image and creating a System.Windows.Controls.Image 将System.Drawing.Icon转换为System.Windows.Controls.Image - Converting System.Drawing.Icon to System.Windows.Controls.Image 如何将 system.windows.controls.image 转换为 byte[] - How to Convert system.windows.controls.image to byte[] 在运行时将 byte[] 加载到 System.Windows.Controls.Image 中 - Load byte[] into a System.Windows.Controls.Image at Runtime 将System.Windows.Controls.Image加载到WritableBitmap对象 - Loading System.Windows.Controls.Image to WritableBitmap object 如何以System.Windows.Controls.Image的字符串形式获取路径 - How to get the path in string form of System.Windows.Controls.Image
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM