I am trying to display a picture loaded by OpenFileDialog in a new window.
The first way, which is doing that entirely in code works. I want to do it in another way, though. I attempt to add that image to resources and then use this for a given control.
XAML part:
<Window.Resources>
<Image x:Key="imageResource">
<Image.Source>
<BitmapImage UriSource="{Binding ImageUri}" />
</Image.Source>
</Image>
</Window.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Image Name="image" />
<Image Name="image2" Grid.Column="1" Source="{Binding imageResource}" />
</Grid>
C# code:
public partial class PicWindow : Window
{
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String propertyName = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
private Uri imageUri;
public Uri ImageUri
{
get
{
return imageUri;
}
set
{
imageUri = value;
NotifyPropertyChanged("ImageUri");
}
}
public PicWindow()
{
InitializeComponent();
}
public PicWindow(string filePath)
{
InitializeComponent();
imageUri = new Uri(filePath);
image.Source = new BitmapImage(imageUri);
}
}
Left column (done in the c# code) displays a loaded picture, but the right one does not (the one with binding usage).
P.S. Regarding that every new picture is opened in a new window I realise that the ImageUri change notification is redundant in here. Please ignore this.