按钮命令未绑定到 DatGrid 中的 ViewModel
我有 ViewModel,它有一个 ObservableCollection[Employee] EmpCol ,现在 ViewModel 与我的 View 绑定,并且 EmpCol 设置为自定义控件的 ItemSource。该自定义控件生成带有 grid 的 stackpanels ,如果 EmpCol 中有 4 个对象,那么将有 4 个带有 Grid 的 stackpanels 。现在在这些网格中,我有一列按钮。 现在我的问题是我无法将数据网格中该按钮的命令绑定到 Employee 类中的 RelayCommand 。
我正在使用 MVVM light 工具包,我找到了一种方法,实际上可以提供像这样的绑定命令的完整路径
<Button x:Name="myButton" Width="20" Height="15" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="5" >
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<cmd:EventToCommand Command="{Binding Source={StaticResource VMLocator}, Path=MyVM.Employee.MyButtonCommand}" PassEventArgsToCommand="False"/> </i:EventTrigger>
</i:Interaction.Triggers>
</Button>
现在上述方法有效,但在我的情况下,我没有 Employee ,而是 Employee 的集合,所以当我给出绑定时像这样的路径 Path=MyVM.EmpCol.MyButtonCommand
它不起作用。我进行了很多搜索,但找不到任何解决方案。 因此,任何帮助解决我的问题将不胜感激。
谢谢, Maverick
命令绑定发生在DataGrid 的一行内。
I have ViewModel, Which has a ObservableCollection[Employee] EmpCol , now that ViewModel is bind with my View and and EmpCol is set as with ItemSource of custom control. That custom control generates stackpanels with grids , if there are 4 objects in EmpCol then there will be 4 stackpanels with Grids inside them. Now in those grids, I have a column of Buttons.
Now my problem is I can't bind Command of that Button in datagrid to RelayCommand in Employee class.
I am using MVVM light toolkit, and I found a way around which actually kind of give full path to binding command like this
<Button x:Name="myButton" Width="20" Height="15" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="5" >
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<cmd:EventToCommand Command="{Binding Source={StaticResource VMLocator}, Path=MyVM.Employee.MyButtonCommand}" PassEventArgsToCommand="False"/> </i:EventTrigger>
</i:Interaction.Triggers>
</Button>
Now this above approach works , but in my case I dont have Employee , but collection of Employee, so when I give binding path like this
Path=MyVM.EmpCol.MyButtonCommand
It does't work. I have searched a lot , but could't find any solution.
So any help will be much appreciated to solve my problem.
Thanks,
Maverick
The command binding happens inside a row of a DataGrid.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
解决方案取决于视图的结构,如果您有主详细信息视图,则应该创建一个子结构,该子结构需要员工 ViewModel 作为 DataContext 并将其绑定到当前选定的员工。这样您就可以获得适合您的视图的正确 ViewModel。
如果您导航到新页面,最简单的方法可能是向视图传递您想要显示的员工的 ID,然后在后面的代码中调用视图模型上的方法(您可以从 DataContext 属性获取该方法)来加载来自数据库或服务的正确员工。
或者,您可以将当前员工保存在应用程序范围内的可访问变量中。例如,应用程序类上的静态变量、存储在资源中或 ViewLocator 上的单例或静态类(在所有情况下,当没有选择员工或阻止打开视图时,您可能必须提供一个空的员工 ViewModel)。
编辑:
如果 ViewModel 集合绑定到 DataGrid 并且命令绑定存在问题,这可能是一个可能的解决方案:
将命令放在包含 Collection 的 ViewModel 上(即属性)绑定到 DataGrid 的 ItemsSource)。然后在这个高级 ViewModel 上执行该命令。现在,您可以使用RelativeSurce 绑定通过DataGrid 的父DataContext 属性访问此更高级别的ViewModel。在按钮中,您现在可以绑定命令并将当前行的 DataContext 作为命令参数传递 - 该命令显然需要此参数。
编辑2:
忘记了Silverlight并不支持所有RelativeSource模式,但是,它支持绑定到命名元素,因此以下内容应该有效...
假设:
代码:
The tsolution depends on the structure of your view, if you have a master detail view you should create a sub-structure that expects an employee ViewModel as a DataContext and bind that to the currently selected employee. With that you get the correct ViewModel for your view.
If you navigate to a new page the easiest approach might be to to pass the View the id of the employee you want to show and then in code behind call a method on you view model - which you can get from the DataContext propery - to load the correct employee from the database or service.
Alternatively, you can hold the current employee in a application wide acessible variable. E.g. a static variable on your application class, a singleton or static class stored in you resources, or on your ViewLocator (in all cases you may have to provide an empty employee ViewModel when no employee is selected or prevent the opening of the View).
Edit:
If a collection of ViewModels is bound to a DataGrid and binding of the command is a problem, this might be a possible solution:
Put the command on a ViewModel that contains the Collection (i.e.the propety that is bound to the ItemsSource of the DataGrid). Then implement the command on this high level ViewModel. You now can use a RelativeSurce binding to access this higher level ViewModel via the DataGrid's parent DataContext property. In your button you can now bind the command and pass the current row's DataContext as the command parameter - the command obviously has to expect this parameter.
Edit 2:
Forgot Silverlight does not support all RelativeSource modes, however, it supports binding to a named element, so the following should work ...
Assumptions:
Code:
最简单的方法是使用 Relay 命令,例如
详细信息,您可以在以下链接中看到
http://codenicely.blogspot.com/2012/01/handling-button-click-event-in.html
The easiest way is using the Relay command for example
for Detail you can see at following link
http://codenicely.blogspot.com/2012/01/handling-button-click-event-in.html