-1

我有一个像这样的 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 数据模板中的按钮时,我想获取 textblock txt 的内容,以找出单击了哪个列表框项以追溯列表框绑定的列表(模型)中的索引。但是从命令参数我什么都得不到,因为我认为有很多名为 txt 的文本块。

请帮我 !

4

1 回答 1

0

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

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

这样,绑定不依赖于存在且具有特定名称的特定控件。

于 2011-11-14T19:40:53.550 回答