简体   繁体   English

绑定图像控件的“源”属性不起作用

[英]Binding 'Source' property of Image control not working

I am trying to bind Source property of Image control through XAML . 我正在尝试通过XAML绑定Image控件的Source属性。 But nothing shows up in my Image control. 但是我的Image控件中没有任何显示。

XAML: XAML:

<Window x:Class="ImageSourceBinding.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <StackPanel>
        <Image Name="MyImage1" Width="32" Height="32" Source="{Binding MyImageSource}"/>
        <Image Name="MyImage2" Width="32" Height="32" />
    </StackPanel>
</Window>

Code Behind: 背后的代码:

public partial class MainWindow : Window, INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    ImageSource MyImageSource;
    String      FilePath = @"C:\Users\UserName\Documents\Map.txt";

    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = this;

        System.Drawing.Icon ExtractedIcon = System.Drawing.Icon.ExtractAssociatedIcon(FilePath);
        MyImageSource = Imaging.CreateBitmapSourceFromHIcon(ExtractedIcon.Handle, new Int32Rect(0, 0, 32, 32), BitmapSizeOptions.FromEmptyOptions());
        ExtractedIcon.Dispose();

        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs("MyImageSource"));

        this.Icon       = MyImageSource;  //This is Working
        //MyImage2.Source = MyImageSource;  //and this too.
    }
}

I can use MyImageSource to change Icon of Window. 我可以使用MyImageSource更改“窗口”图标。 Also, if I set Source from Code-Behind the image is correctly displayed. 另外,如果我从“代码隐藏”中设置“源”,则可以正确显示图像。

Since you are using PropertyChanged and apparently the correct data context, I assume that your binding gets notified about the change of MyImageSource , but simply cannot access it, since MyImageSource is private and not a property. 由于您使用的是PropertyChanged,而且显然使用的是正确的数据上下文,因此我假设您的绑定已收到有关MyImageSource更改的MyImageSource ,但由于MyImageSource是私有的而不是属性,因此无法访问它。

Try using this to define MyImageSource : 尝试使用它来定义MyImageSource

public ImageSource MyImageSource { get; private set; }

You can't bind to a field, only to a property. 您不能绑定到字段,而只能绑定到属性。 Write a property like this and just assign to the property in code behind: 编写这样的属性,然后在后面的代码中将其分配给该属性:

private ImageSource _myImageSource;
public ImageSource MyImageSource {
    get { return _myImageSource; }
    set {
        if (value != _myImageSource)
        {
            _myImageSource = value;
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(MyImageSource)));
        }
    }
}

public MainWindow()
{
    InitializeComponent();
    this.DataContext = this;

    System.Drawing.Icon ExtractedIcon = System.Drawing.Icon.ExtractAssociatedIcon(FilePath);
    MyImageSource = Imaging.CreateBitmapSourceFromHIcon(ExtractedIcon.Handle, new Int32Rect(0, 0, 32, 32), BitmapSizeOptions.FromEmptyOptions());
    ExtractedIcon.Dispose();

    this.Icon       = MyImageSource;  //This is Working
}

Now your binding should work. 现在您的绑定应该起作用了。

DataContext = this; is not recommended. 不推荐。 It's preferred to have a separate viewmodel class. 最好有一个单独的viewmodel类。 But it won't kill you this once. 但这不会一次杀死你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM