I have basic problem with WPF, I tried setting DataContext
, binding to collection and somehow I still cannot get it to work. I searched way to long for such basic problem... I must be missing something very simple. I appreciate any help :)
Code-behind:
public ObservableCollection<Photo> MyPhotos = new ObservableCollection<Photo>();
public DataTemplate()
{
InitializeComponent();
listBox.DataContext = MyPhotos;
MyPhotos.Add(new Photo(@"path to existing file"));
}
XAML
<Window.Resources>
<DataTemplate DataType="{x:Type local:Photo}">
<Border Margin="3">
<Image Source="{Binding Source}"/>
</Border>
</DataTemplate>
</Window.Resources>
<Grid>
<ListBox Name="listBox" ItemsSource="{Binding MyPhotos}" Background="Silver" Width="600" Margin="10" SelectedIndex="0"/>
</Grid>
Photo class:
public class Photo
{
public string Source { get; set; }
public Photo(string path)
{
Source = path;
}
}