【问题标题】:Binding a wpf xaml image to System.Windows.Controls.Image is not working将 wpf xaml 图像绑定到 System.Windows.Controls.Image 不起作用
【发布时间】:2020-11-01 06:56:25
【问题描述】:

我在 wpf 应用程序中有一个图像并将其 Source 属性绑定到视图模型中的 System.Windows.Controls.Image 但它不起作用。但是当我将其 Source 属性绑定到 BitmapImage 时,它​​正在工作。有没有办法将 Source 属性绑定为 System.Windows.Controls.Image ?

Xaml 部分

  <Image x:Name="ShipMainImage" Source="{Binding MainImageSource}" />

查看模型部分

 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;

}

【问题讨论】:

    标签: c# wpf image bitmapimage


    【解决方案1】:

    不要使用System.Windows.Controls.Image 作为该属性的类型。它是一种在视图中使用的 UI 元素类型,但不在视图模型中。

    将属性声明为System.Windows.Media.ImageSource - Image 元素的Source 属性的类型:

    private ImageSource mainImageSource;
    
    public ImageSource MainImageSource
    {
        get { return mainImageSource; }
        set
        {
            mainImageSource = value;
            OnPropertyChanged(nameof(MainImageSource));
        }
    }
    

    【讨论】:

    • 感谢@Clemens 的回答。我的问题是为什么将 xaml 图像 Source 属性绑定到 System.Windows.Controls.Image 不起作用。只有 Source 属性与 BitmapImage 的绑定有效。我更新了我的问题,请看一下。
    猜你喜欢
    • 2014-09-09
    • 1970-01-01
    • 2021-11-26
    • 2016-04-10
    • 1970-01-01
    • 1970-01-01
    • 2016-10-03
    • 1970-01-01
    • 2014-06-18
    相关资源
    最近更新 更多