【问题标题】:Get content of control inside listbox item when it was clicked单击时获取列表框项内的控件内容
【发布时间】:2026-02-18 22:00:02
【问题描述】:

我有一个这样的 ItemTemplate

<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>

当我单击 listboxitem datatemplate 中的按钮时,我想获取 textblock txt 的内容,以找出单击了哪个列表框项目以追溯列表框绑定的 List(模型)中的索引。 但是从命令参数中我什么也得不到,因为我认为有很多名为 txt 的文本块。

请帮帮我!

【问题讨论】:

    标签: windows-phone-7 data-binding commandparameter


    【解决方案1】:

    解决此问题的更好方法是将 CommandParameter 绑定到项目的 DataContext 中的“事物”,如下所示:

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

    这种方式绑定不依赖于存在且具有特定名称的特定控件。

    【讨论】: