I’m binding the image Source to a BitmapImage in my code but it doesn’t show up.
xaml:
<Window x:Class="bleh.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow"
mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" d:DesignHeight="600" d:DesignWidth="600">
<Grid x:Name="LayoutRoot">
<Image x:Name="current" HorizontalAlignment="Center" Stretch="None" VerticalAlignment="Center" Source="{Binding Picture}" />
</Grid>
</Window>
and my cs:
public partial class MainWindow : Window, INotifyPropertyChanged
{
/// <summary>
/// Event implementing INotifyPropertyChanged interface.
/// </summary>
public event PropertyChangedEventHandler PropertyChanged;
public BitmapImage Picture { get; set; }
public MainWindow()
{
Uri uri = new Uri("Images/xpto.jpg", UriKind.Relative);
this.Picture = new BitmapImage(uri);
InitializeComponent();
//setup();
}
}
Strangely enough, the window opens with the size of the image, but I don’t see the image. I’ve also tried assigning manually in the xaml and it works.
Doing current.source="Images/xpto.jpg" also works.
The DataContext of your view (MainWindow) is not set, so there is no “Picture” property to which to bind. If you want the view to bind to itself, add
to your MainWindow constructor.