【问题标题】:WPF c# Retrieving a listbox row's contents from a button in the same datatemplateWPF c#从同一数据模板中的按钮检索列表框行的内容
【发布时间】:2011-03-18 16:45:20
【问题描述】:

我已经研究了一段时间但无济于事(我试图让命令工作,但我认为我严重错过了一些东西。WPF 的做事方式非常新,但没有帮助(但真的很喜欢我在 WPF 中找到的东西)

我和这篇文章有同样的问题: Get the ListBox row object from a button that is on the DataTemplate

我在每个数据模板中有一个带有标签 + 2 个按钮的列表框。单击按钮时,我需要知道单击了哪一行。 (该行根本没有被选中,所以 Selectedindex 是不可能的)。我真的很喜欢 Vaccano 的方法,但似乎无法让它在他提供的信息上发挥作用(他自己说它非常简短)

<ListBox x:Name="lbDirectoryTreeBox" Height="auto" Width="auto" Grid.Column="0" Grid.Row="0" Margin="10,10,5,10" ItemsSource="{Binding}" 
            MouseDoubleClick="lbDirectoryBox_MouseDoubleClick" SelectionChanged="lbDirectoryTreeBox_SelectionChanged">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">                        
                <Button x:Name="btnOpenFolder" Content="R" Width="20" Height="20" Click="btnDirectoryOpen_Click" />
                <Button x:Name="btnAddFolder" Content="R" Width="20" Height="20" Click="btnAddFolder_Click" />
                <Label Width="auto" Content="{Binding Path=Name}" />
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

单击任一按钮后如何获取该行的选定索引? (在 C# 中)

在此先感谢您提供的任何帮助。

【问题讨论】:

    标签: c# wpf listbox datatemplate


    【解决方案1】:

    数据模板中的按钮将从列表框项继承数据上下文,因此您可以使用如下内容:

    private void btnDirectoryOpen_Click(object sender, RoutedEventArgs e)
    {
        var item = ((Button) sender).DataContext;
        var itemIndex = lbDirectoryTreeBox.Items.IndexOf(item);
        // ...
    }
    

    【讨论】:

    • 在尝试了这个方法和命令方法之后,我已经使用了这个方法。这有什么缺点吗? @Gimno 方法是上一篇文章尝试的方法,如果它真的这么简单,这似乎很奇怪。
    • @Oli:Gimno 的 FindAncestor 绑定方法让您可以立即访问项目的容器;使用所描述的方法,您将不得不使用额外的代码来找到它 - 例如使用 lbDirectoryTreeBox.ItemContainerGenerator 的 ContainerFromItem/ContainerFromIndex 方法,或者通过执行可视父查找。
    【解决方案2】:

    您可以使用CommandParameter 来获取您想要的行:

    CommandParameter="{Binding Path=Content, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}}}

    所以,如果你想使用它,你必须为你的按钮分配一个命令并将其用作命令参数,例如:

    &lt;Button x:Name="btnOpenFolder" Content="R" Width="20" Height="20" Command="Help" CommandParameter="{Binding Path=Content, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}}}" /&gt;

    您必须为您的窗口/页面/用户控件创建 CommandBindings,例如:

    <Window.CommandBindings>
        <CommandBinding Command="Help" Executed="btnDirectoryOpen_Command" />
    </Window.CommandBindings>
    

    (这里使用“帮助”作为命令不是一个很好的例子,你应该用更好的东西代替它)

    在 CodeBehindFile 中的方法中,您可以通过 e.Parameter 获取 ListBoxItem:

    private void btnDirectoryOpen_Command(object sender, ExecutedRoutedEventArgs e)
    {
      var item = e.Parameter;  
    }
    

    进一步阅读:How to enable commands

    【讨论】:

      【解决方案3】:

      如果您的行数有限,您可以将 AlternationIndex 设置为非常高的值并将其用作命令参数

      <Button CommandParameter="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=(ItemsControl.AlternationIndex)}" />
      

      【讨论】:

        猜你喜欢
        • 2011-05-30
        • 2018-06-30
        • 1970-01-01
        • 1970-01-01
        • 2013-07-14
        • 1970-01-01
        • 1970-01-01
        • 2011-06-08
        • 1970-01-01
        相关资源
        最近更新 更多