WPF MVVM - 一个ItemsControl的命令结合内 [英] WPF MVVM - Command binding inside of an ItemsControl

查看:248
本文介绍了WPF MVVM - 一个ItemsControl的命令结合内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在转换一个小WPF项目MVVM。我有一个名单,其中,CustomObject> 在我ItemsControl的结合,并使用一个DataTemplate打造每个元素的UI主窗口的视图模型。我的老code使用的事件处理程序的DataTemplate内处理click事件。我想使用某种命令的结合,以消除我的code隐藏事件处理程序,但在我的ItemsControl项目的DataContext的是模型对象,所以我目前还不能绑定到一个ICommand从视图模型。

所以,我想有几个方法来攻击这一点,我不知道这将是这样做的最MVVM的方式。难道我的ItemsControl.ItemsSource绑定到重新presents的每一项新的ViewModel类的集合?还是我使用用户控件,而不是一个DataTemplate,然后我可以每个用户控件绑定到一个视图模型的重新presents它它自己的实例吗?还是有某种约束力的前pression我可以用它来指窗口回的DataContext有机会获得绑定到视图模型(我键入此,它只是听起来很糟糕,所以我假设一个大的 NO这种想法)?

另外,我想我的命令绑定到一个网格控件的LeftMouseButtonUp事件。有没有命令一格,所以我试图用InputBindings。我可以用一个静态的命令(如一个内置的ApplicationCommands),但我不能用绑定EX pression绑定到一个ICommand的实例,它是视图模型的属性,因为MouseBinding.Command不是的DependencyProperty

我是pretty的困惑事件处理在MVVM的主题,所以任何和所有的信息是AP preciated。

解决方案
  

我的ItemsControl.ItemsSource绑定到重新presents一个新的ViewModel类的集合,每个项目?

无论你创建一个CustomObjectViewModel主办的命令或将同一视图模型具有列表实际上取决于行动发生的文化功能中的命令。有什么事情,与CustomObject所属,或者是它的东西,与当前的视图模型属于?

  

还是有某种约束力的前pression我可以用它来指窗口回的DataContext有机会获得绑定到视图模型(我键入此,它只是听起来很糟糕,所以我假设一个大大的NO这个想法)?

这并不像听起来那么糟糕。你不是真的从中切换之前,个别项目所需要的窗口的DataContext的,只是在DataContext。所以,如果您的命令是在承载CustomObjects的名单相同的视图模型,你可以结合它在使用这两种方法CustomObject的DataTemplate中的一个:

  {绑定的ElementName = uiCustomObjectsItemsControl,路径= DataContext.MyCommand}
{绑定的RelativeSource = {的RelativeSource模式= FindAncestor,AncestorType = {X:类型的ItemsControl}},路径= DataContext.MyCommand}
 

  

另外,我想结合我的命令   到是一个在LeftMouseButtonUp事件   网格控件。有没有命令   一个网格,所以我试图用   InputBindings。

对于这一点,我会使用类似附加命令行为< /一>这将让你附加的ICommand对任何事件。

I'm currently converting a small WPF project to MVVM. I have a List<CustomObject> in the ViewModel of the main window that my ItemsControl binds to and uses a DataTemplate to build the UI of each element. My old code used an event handler inside the DataTemplate to handle a click event. I want to use some kind of command binding to eliminate my code-behind event handlers, but the DataContext of the items in my ItemsControl is the model object so I can't currently bind to an ICommand from the ViewModel.

So, I guess there are a couple of ways to attack this and I'm not sure which would be the most "MVVM" way of doing it. Do I bind the ItemsControl.ItemsSource to a collection of a new ViewModel class that represents each item? Or do I use UserControls instead of a DataTemplate and then I can bind each UserControl to it's own instance of a ViewModel that represents it? Or is there some kind of binding expression I can use to refer back to the DataContext of the window to have access to bind to the ViewModel (as I type this, it just sounds bad so I'm assuming a big "NO" to this idea)?

Also, what I want to bind my command to is the LeftMouseButtonUp event of a Grid control. There's no "Command" for a Grid, so I was attempting to use InputBindings. I could use a static command (such as one of the built in ApplicationCommands), but I could not use a binding expression to bind to an ICommand instance that is a property of the ViewModel because MouseBinding.Command is not a DependencyProperty.

I'm pretty confused on the subject of event handling in MVVM, so any and all info is appreciated.

解决方案

Do I bind the ItemsControl.ItemsSource to a collection of a new ViewModel class that represents each item?

Whether you create a CustomObjectViewModel to host the command or place the command within the same ViewModel that has the list really depends on the function of the action occuring. Is it something that belongs with the CustomObject, or is it something that belongs with your current ViewModel?

Or is there some kind of binding expression I can use to refer back to the DataContext of the window to have access to bind to the ViewModel (as I type this, it just sounds bad so I'm assuming a big "NO" to this idea)?

This isn't as bad as it sounds. You don't really need the DataContext of the Window, just the DataContext from before it switched over to the individual items. So if your command was on the same ViewModel that hosts the List of CustomObjects, you could bind to it from within one of the CustomObject's DataTemplate using either of these methods:

{Binding ElementName=uiCustomObjectsItemsControl, Path=DataContext.MyCommand}
{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ItemsControl}}, Path=DataContext.MyCommand}

Also, what I want to bind my command to is the LeftMouseButtonUp event of a Grid control. There's no "Command" for a Grid, so I was attempting to use InputBindings.

For this, I would use something like Attached Command Behaviors which will let you attach a ICommand to any event.

这篇关于WPF MVVM - 一个ItemsControl的命令结合内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆