Given this piece of code, I want to have access to the properties (more specifically, its index) of the ListBoxItem
that contains a Button
when I press it. I've figured that I need to obtain the Button
's parent (which is an StackPanel
) and then its TemplatedParent
and then walk up the Visual Tree, but I don't know two very important things: how to do that efficiently to solve my problem, neither whether that really solves my problem.
<ListBox x:Name="myListBox" Foreground="Black" HorizontalAlignment="Left" Height="101" Margin="237,312,0,0" VerticalAlignment="Top" Width="420">
<ListBox.ItemTemplate>
<!--src is the alias I gave to the xmlns that gives me access to the assembly on which I'm working-->
<DataTemplate DataType="{x:Type src:Items}">
<DataTemplate.Resources>
<Style TargetType="Label">
<Setter Property="Foreground" Value="Black"/>
<Setter Property="FontFamily" Value="Tahoma"/>
</Style>
</DataTemplate.Resources>
<StackPanel Orientation="Horizontal">
<!--Property1 to Property3 are defined in the Items type-->
<Label Content="{Binding Path=Property1}" Width="30" MaxWidth="30"/>
<Label Content="{Binding Path=Property2}" Width="100" MaxWidth="100"/>
<Label Content="{Binding Path=Property3}" Width="250" MaxWidth="250"/>
<Button Content="X" Width="30" MaxWidth="30" Click="RemoveItemFromList"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
//Somewhere in my code
ObservableCollection<Items> myStuff = createListOfItems();
myListBox.DataContext = myStuff;
myListBox.setBinding(ItemsControl.ItemsSourceProperty, new Binding());
private void RemoveItemFromList(object sender, RoutedEventArgs e)
{
int index = (((sender as Button).Parent as StackPanel).TemplatedParent as ListBoxItem).IsSelected = true;
myStuff.RemoveAt(myListBox.SelectedIndex);
}
ItemsControl
has a utility method you can use called ContainerFromElement
. You pass in an element from the visual subtree of an item container, and it gives you the container, e.g., the ListBoxItem
:
var container = ItemsControl.ContainerFromElement(myListBox, sender as UIElement)
as ListBoxItem;