MainWindow.xaml<Window x:Class="WpfApplication3.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:WpfApplication3"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Image Name="image" HorizontalAlignment="Left" Height="282" Margin="18,18,0,0" VerticalAlignment="Top" Width="394" Source="{Binding MySource}"/>
<Button Name="button" Content="Button" HorizontalAlignment="Left" Margin="432,18,0,0" VerticalAlignment="Top" Width="75" Click="button_Click"/>
</Grid>
</Window>
MainWindow.xaml.csusing Microsoft.Win32;
using System;
using System.ComponentModel;
using System.Windows;
namespace WpfApplication3
{
public partial class MainWindow : Window
{
public MyClass Class;
public MainWindow()
{
InitializeComponent();
Class = new MyClass();
image.DataContext = Class;
}
private void button_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog myDialog = new OpenFileDialog();
if (myDialog.ShowDialog().Value)
Class.MySource = myDialog.FileName;
}
}
public class MyClass : INotifyPropertyChanged
{
private String mySource;
public String MySource
{
get
{
return mySource;
}
set
{
mySource = value;
OnPropertyChanged("MySource");
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}