WPF c# 从同一数据模板中的按钮检索列表框行的内容

发布于 2024-10-23 18:59:12 字数 1333 浏览 3 评论 0原文

我已经研究这个有一段时间了,但无济于事(我试图让命令工作,但我认为我严重错过了一些东西。对于 WPF 的做事方式非常陌生,但没有帮助(但真的)喜欢我在 WPF 中发现的内容)

我遇到了与这篇文章相同的问题: 获取列表框行来自 DataTemplate 上按钮的对象

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

<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# 中)

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

I've been researching this for a while and to no avail (I've tried to get command working but there is somethign I'm seriously missing I think. Very new to WPF's way of doing things which isn't helping (but really liking what I find with WPF)

I'm have the same issue as this post:
Get the ListBox row object from a button that is on the DataTemplate

I have a listbox with labels + 2 buttons in each datatemplate. I need t know which row is being clicked when the button is clicked. (The row is not selected at all so Selectedindex is out the question). I really like Vaccano's method but can't seem to get it to work on the informatin he has given (he states himself it is very brief)

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

How can I get the selectedindex of the row after clicking either of the buttons? (in c#)

Thanks in advance for any help here.

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

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

发布评论

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

评论(3

戏剧牡丹亭 2024-10-30 18:59:12

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

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

Buttons in the data template will inherit data context from the list box item, so you can use something like:

private void btnDirectoryOpen_Click(object sender, RoutedEventArgs e)
{
    var item = ((Button) sender).DataContext;
    var itemIndex = lbDirectoryTreeBox.Items.IndexOf(item);
    // ...
}
笑红尘 2024-10-30 18:59:12

您可以使用 CommandParameter 获取所需的行:

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

因此,如果您想使用它,您必须为按钮分配一个命令并将其用作命令参数,例如:

并且您必须创建 CommandBindings对于您的窗口/页面/用户控件,例如:(

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

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

在 CodeBehind- 文件中的方法中,您可以使用 e 获取 ListBoxItem。参数:

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

进一步阅读:如何启用命令

You can use a CommandParameter to get the row you want:

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

So, if you want to use this, you have to assign a Command to your button and use it as command parameter, e.g.:

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

and you have to create CommandBindings for your Window/Page/UserControl, for example:

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

(Using "Help" as command here is not a good example, you should replace it with something better)

In your method in the CodeBehind- File, you can get the ListBoxItem with e.Parameter:

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

Further readings: How to enable commands

帅的被狗咬 2024-10-30 18:59:12

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

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

If you have a limited number of rows you could set the AlternationIndex to something really high and use that as a CommandParameter

<Button CommandParameter="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=(ItemsControl.AlternationIndex)}" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文