У меня есть отдельный класс PicContainer с полем типа Bitmap. Как можно привязать это поле к контролу "image"? Код класса PicContainer:
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));
}
Главная форма:
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:
<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>
Результат 0. Как нужно изменить XAML разметку, чтобы картинка загрузилась?
set { image = value; } }а где вы тут вызываете OnPropertyChanged()? – Bulson Mar 01 '17 at 18:45Bitmap? Это случайно не Winform'овскийSystem.Drawing.Bitmap? – VladD Mar 01 '17 at 21:14