WPF中可点击的ItemsControl项目 [英] Clickable ItemsControl item in WPF
问题描述
我使用ItemsControl来显示数据绑定项目的列表,每个DataTemplate的核心都是一个Grid,我已经放置了所有绑定的控件。
我希望能够点击列表中每个项目的整个区域。但我无法弄清楚如何使区域可点击。
关于如何使整个网格区域可点击的任何建议都非常棒。
您可以使用来实现这一点。
在ViewModel中定义一个命令,然后在Grid对象上使用ACB AttachedProperties将 MouseLeftButtonUp
事件绑定到命令。
一些代码让你开始:
< Grid Name =gridHeight =30ForceCursor =TrueCursor =Hand>
< acb:CommandBehaviorCollection.Behaviors>
将ACB:BehaviorBinding事件= 的MouseLeftButtonUp 命令= {绑定路径= DataContext.EditEventCommand,的RelativeSource = {的RelativeSource AncestorType = {X:类型自:控制板}}} CommandParameter = {结合}/ >
< / acb:CommandBehaviorCollection.Behaviors>
< / Grid>
针对非MVVM解决方案的编辑。 当你没有根据MVVM指南设计你的应用程序时,上面的代码片段仍然可以工作,因为你本质上只是绑定到一个命令代码隐藏。 然而,如果你不想去定义命令的麻烦,你可以简单地指定一个事件来挂钩,就像这样:
<网格的MouseLeftButtonUp = Grid_MouseLeftButtonUp >
在XAML文件
'p>和在代码隐藏:
私人无效Grid_MouseLeftButtonUp(对象发件人,MouseButtonEventArgs E)
{
}
I'm using an ItemsControl to display a list of databound items, for each the core of the DataTemplate is a Grid upon which I've placed all the bound controls.
I would like to be able to click on the entire area for each item in the list. But I cannot figure out how to make the area clickable.
Any suggestions of how to make an entire grid area clickable would be great.
You can use the AttachedCommandBehavior classes from C# Disciples to achieve this.
Define a command in the ViewModel, and then on the Grid object use the ACB AttachedProperties to bind the MouseLeftButtonUp
event to the command.
Some code to get you started:
<Grid Name="grid" Height="30" ForceCursor="True" Cursor="Hand">
<acb:CommandBehaviorCollection.Behaviors>
<acb:BehaviorBinding Event="MouseLeftButtonUp" Command="{Binding Path=DataContext.EditEventCommand, RelativeSource={RelativeSource AncestorType={x:Type self:Dashboard}}}" CommandParameter="{Binding}" />
</acb:CommandBehaviorCollection.Behaviors>
</Grid>
Edit for non-MVVM solution.
The above code snippet will still work when you have not designed your application following the MVVM guide-lines as you are essentially just binding to a command in the code-behind.
However, if you don't want to go to the trouble of defining commands, you can simply specify an event to hook to, like so:
<Grid MouseLeftButtonUp="Grid_MouseLeftButtonUp">
in the XAML file.
and in the code-behind:
private void Grid_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
}
这篇关于WPF中可点击的ItemsControl项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!