WPF中可点击的ItemsControl项目

编程入门 行业动态 更新时间:2024-10-27 23:32:33
本文介绍了WPF中可点击的ItemsControl项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我使用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文件

$ b $。 b '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项目

本文发布于:2023-11-12 03:01:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1580318.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:项目   WPF   ItemsControl

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!