Get content of control inside listbox item when it was clicked

655 Views Asked by At

I have a ItemTemplate like this

<ListBox>
    <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition />
                            <RowDefinition />
                        </Grid.RowDefinitions>
                        <Button Grid.Row="0">
                            <Button.Template>
                                <ControlTemplate>
                                    <Border BorderBrush="AntiqueWhite" BorderThickness="1">
                                        <StackPanel Orientation="Horizontal" Background="Gray" Width="465">
                                            <Image Margin="2,0,10,0" Source="{Binding StateImage}" Stretch="None"/>
                                            <TextBlock Name="txt" Text="{Binding DateLabel}"/>
                                        </StackPanel>
                                    </Border>
                                </ControlTemplate>
                            </Button.Template>
                            <Custom:Interaction.Triggers>
                                <Custom:EventTrigger EventName="Click">
                                    <mx:EventToCommand Command="{Binding VisibilityListboxCommand}"
                                                       CommandParameter="{Binding EementName=txt, Path=Text}"
                                                           />
                                </Custom:EventTrigger>
                            </Custom:Interaction.Triggers>
                        </Button>
                        <ListBox Grid.Column="1" ItemsSource="{Binding WhatsonList, Mode=OneWay}">
                            <ListBox.ItemTemplate>
                                <DataTemplate>
                                    <Border BorderBrush="Gray" BorderThickness="0,0,0,1" Padding="0,0,0,10">
                                        <Grid Margin="0,10,0,0">
                                            <Grid.ColumnDefinitions>
                                                <ColumnDefinition Width="100"/>
                                                <ColumnDefinition Width="360"/>
                                            </Grid.ColumnDefinitions>
                                            <StackPanel Grid.Column="0">
                                                <Button>
                                                    .....
                                                </Button>
                                                <CheckBox Template="{StaticResource CheckboxImageTemplate}" Margin="0,5,0,0"/>
                                            </StackPanel>
                                            <ListBox Grid.Column="1">
                                                .....
                                            </ListBox>
                                        </Grid>
                                    </Border>
                                </DataTemplate>
                            </ListBox.ItemTemplate>
                        </ListBox>
                    </Grid>
                </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

When I click on button inside listboxitem datatemplate I want to get content of textblock txt to find out what listbox item was clicked to trace back index in the List(model) that listbox bind from. But from commandparameter I cannot get anything because there are many textblock named txt I think.

Please help me !

1

There are 1 best solutions below

0
On

A better way to solve this is by binding the CommandParameter to 'thing' in the DataContext of the item like this:

<Custom:Interaction.Triggers>
    <Custom:EventTrigger EventName="Click">
        <mx:EventToCommand Command="{Binding VisibilityListboxCommand}"
                           CommandParameter="{Binding}" />
    </Custom:EventTrigger>
</Custom:Interaction.Triggers>

This way the binding does not rely on a particular control being present and having a specific name.