单击时获取列表框项目内的控件内容

发布于 2024-12-11 10:41:29 字数 3679 浏览 0 评论 0原文

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

请帮我 !

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 !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一身骄傲 2024-12-18 10:41:29

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

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文