I have a ListBox
that looks like this:
<ListBox Name="ListBoxUsers" ItemsSource="{Binding Path=TempObservableUsers, ElementName=MainWindow, NotifyOnSourceUpdated=True}" ScrollViewer.HorizontalScrollBarVisibility="Hidden" ScrollViewer.VerticalScrollBarVisibility="Hidden" />
This ListBox
shows, as you can see, a list of users comming from an ObservableCollection
called TempObservableUsers. This part works fine.
Each element of the list looks like this:
As you can see, I modified my ItempsPanelTemplate
to look differently. Here I attach my code:
<Style TargetType="ListBox" x:Key="VerticalListBox">
<Setter Property="Margin" Value="0,0,10,0"></Setter>
<Setter Property="Background" Value="Transparent" />
<Setter Property="VerticalContentAlignment" Value="Top" />
<Setter Property="IsTabStop" Value="False" />
<Setter Property="BorderThickness" Value="0" />
<Setter Property="BorderBrush" Value="{DynamicResource RegularBlue}" />
<Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
<Setter Property="Width" Value="450" />
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<StackPanel Orientation="Vertical" Width="450" />
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
<Setter Property="ItemTemplate">
<Setter.Value>
<DataTemplate>
<StackPanel Orientation="Vertical" Width="450" Margin="0,5" >
<StackPanel Orientation="Horizontal" Background="{DynamicResource TransparentMainBlue}" >
<Image Width="70" Height="70" Margin="5" Source="../images/delete.png" />
<StackPanel Orientation="Vertical" Width="285" >
<StackPanel Orientation="Horizontal">
<Label Content="NAME:" FontWeight="Bold"/>
<Label Content="{Binding Path=Name, FallbackValue=Name}" Foreground="White" />
</StackPanel>
<StackPanel Orientation="Horizontal">
<Label Content="ID:" FontWeight="Bold"/>
<Label Content="{Binding Path=Identifier, FallbackValue=Identifier}" />
</StackPanel>
</StackPanel>
<StackPanel Orientation="Horizontal" Margin="5" HorizontalAlignment="Right" VerticalAlignment="Top">
<Image Source="../images/edit.png" Width="20" />
<Image Source="../images/detail.png" Width="20" />
<Image Source="../images/delete.png" Width="20" />
</StackPanel>
</StackPanel>
<Grid Height="30" Background="{DynamicResource RegularBlue}">
<Label Content="XXXX-XXXX-XXXX-XXXX" Foreground="White" HorizontalContentAlignment="Center" FontWeight="Bold" />
</Grid>
</StackPanel>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
Ok, after that:
As you can see, each item contains some images on top right corner. I want each one to do its job: edit, more, delete...
So I need to bind each image click with the corresponding item on the ListBox.
How can I bind each image click to do the corresponding action (edit, update, delete) and with its corresponding item on the ListBox
?
EDIT: Now my code looks like this for each item template:
<StackPanel Orientation="Horizontal" Margin="5" HorizontalAlignment="Right" VerticalAlignment="Top">
<Button Command="{Binding DataContext.DeleteCommand}" CommandTarget="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListBox}}" CommandParameter="{Binding}">
<Image Source="../images/delete.png" Width="20" />
</Button>
............
</StackPanel>
How to implement DeleteCommand
?
PS: I'm not using MVVM pattern yet, as I'm not famliar to it. Only trying to bind that button commands i xaml directly from my ResourceDictionary.