I have a separate class PicContainer with a field of type Bitmap. How can I bind this field to the "image" control? PicContainer class code:

class PicContainer : INotifyPropertyChanged { public Bitmap image; public Bitmap Image { get { return image;} set { image = value; } } public String text = "Превет"; public String Text { get { return text;} set { text = value; } } public event PropertyChangedEventHandler PropertyChanged; public void OnPropertyChanged(String propertyName) { if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } 

Main form:

 public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void Button_Click(object sender, RoutedEventArgs e) { PicContainer SimpleClass = new PicContainer(); SimpleClass.Image = new Bitmap("1.jpg"); } } 

XAML code:

 <Window x:Class="imageControl.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:imageControl" mc:Ignorable="d" Title="MainWindow" Height="350" Width="525"> <Window.Resources> </Window.Resources> <Grid> <StackPanel> <Image x:Name="Pic" Margin="5" Source="{Binding ElementName=PicContainer ,Path=SimpleClass.Image}"></Image> <Button x:Name="Button" Content="{Binding Path=Class.Text}" Click="Button_Click" Height="20"></Button> </StackPanel> </Grid> </Window> 

Result 0. How do I change the XAML markup for the picture to load?

  • set { image = value; } } set { image = value; } } where do you call OnPropertyChanged () here? - Bulson
  • And how can I fix it? - Zurus
  • In the property setter, you need to call your OnPropertyChanged () to tell the UI that the value of this property has changed. - Bulson
  • So I answered the person and made for him the framework of the application, and although there is no picture there, but the general sense of working with the properties should be clear to you. - Bulson
  • @Zurus: What is your Bitmap ? Isn't this a Winform System.Drawing.Bitmap ? - VladD

0