PROBLEM:
I have a blank Image control named “Image1“.
Now, I want to supply a Source to that Image at runtime base on the selected item on my ListBox (ListBoxSource).
How will I do that in “ListBoxSource_SelectionChanged(…)” event?
private void ListBoxSource_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
//...Some conditional statement in here which determines what image to set or update its Image source
this.Image1.Source = ??? What to supply in here
}
NOTE: I know how to do this using Binding BUT I know only using another ListBox to show my selected items. This time I use a Grid(2 rows and 2 columns: each cell has a blank Image control in it) for the purpose of supplying each grid cell with the ListBoxSource selected Image item.
OR Can I use some binding to this using Grid? My other purpose is also to be able to control the Image in different sizes (meaning some image will span rows/columns inside a Grid).
Here is my XAML:
<Window.Resources>
<DataTemplate x:Key="ItemTemplate">
<StackPanel>
<Image Source="{Binding FileFullName}" HorizontalAlignment="Left" Height="64" Width="64"/>
<TextBlock Text="{Binding FileName}"/>
</StackPanel>
</DataTemplate>
</Window.Resources>
<Grid x:Name="LayoutRoot" DataContext="{Binding Source={StaticResource SampleDataSource}}">
<ListBox x:Name="ListBoxSource" HorizontalAlignment="Left" ItemTemplate="{DynamicResource ItemTemplate}" ItemsSource="{Binding Collection}" Margin="29,31,0,31" Width="257" SelectionMode="Multiple" SelectionChanged="listBoxSource_SelectionChanged"/>
<Grid x:Name="GridImageHolder" Height="270" Margin="338,44,0,0" VerticalAlignment="Top" Background="#FFE0D6D6" ShowGridLines="True" DataContext="{Binding SelectedItem, ElementName=listBoxSource}" d:DataContext="{Binding Collection[0]}" HorizontalAlignment="Left" Width="539">
<Grid.RowDefinitions>
<RowDefinition Height="130"/>
<RowDefinition Height="140"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="267.883"/>
<ColumnDefinition Width="271.117"/>
</Grid.ColumnDefinitions>
<Image x:Name="Image1" Grid.Row="0" Grid.Column="0" Margin="8,0.96,21.883,8"/>
<Image x:Name="Image2" Grid.Row="0" Grid.Column="1" HorizontalAlignment="Left" Margin="19.975,0,0,8" Width="218"/>
<Image x:Name="Image3" Grid.Row="1" Grid.Column="0" Margin="8,21.04,40.883,16"/>
<Image x:Name="Image4" Grid.Row="1" Grid.Column="1" Margin="8,21.04,33.117,16" />
</Grid>
</Grid>
In the event you need to cast the sender to the appropriate class.