WPF c# 从同一数据模板中的按钮检索列表框行的内容
我已经研究这个有一段时间了,但无济于事(我试图让命令工作,但我认为我严重错过了一些东西。对于 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
数据模板中的按钮将从列表框项目继承数据上下文,因此您可以使用如下内容:
Buttons in the data template will inherit data context from the list box item, so you can use something like:
您可以使用
CommandParameter
获取所需的行:CommandParameter="{Binding Path=Content,relativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}}}
因此,如果您想使用它,您必须为按钮分配一个命令并将其用作命令参数,例如:
并且您必须创建 CommandBindings对于您的窗口/页面/用户控件,例如:(
这里使用“帮助”作为命令不是一个好例子,您应该用更好的东西替换它)
在 CodeBehind- 文件中的方法中,您可以使用 e 获取 ListBoxItem。参数:
进一步阅读:如何启用命令
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:
(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:
Further readings: How to enable commands
如果行数有限,您可以将
AlternationIndex
设置为非常高的值并将其用作 CommandParameterIf you have a limited number of rows you could set the
AlternationIndex
to something really high and use that as a CommandParameter