简体   繁体   中英

Binding 'Source' property of Image control not working

I am trying to bind Source property of Image control through XAML . But nothing shows up in my Image control.

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. 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.

Try using this to define 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. But it won't kill you this once.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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