如何使用MVVM在WPF中使用MenuItem设置绑定数据? [英] How do I setup the binding data with MenuItem in WPF using MVVM ?

查看:107
本文介绍了如何使用MVVM在WPF中使用MenuItem设置绑定数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<DataGrid x:Name="LicenseInventory" ItemsSource="{Binding Path=LicenseInventories}">

< DataGrid.Columns> < DataGridTextColumn Binding =" {Binding Product}" /> < DataGridTextColumn Binding =< {Binding Version}" ></DataGridTextColumn> //其他列,例如激活状态,到期日期等... < DataGridTemplateColumn> < DataGridTemplateColumn.CellTemplate> < DataTemplate> <菜单> < MenuItem Header ="Choose Acions" ItemsSource ="{Binding Path = DataContext.LicenseActions}" DisplayMemberPath ="ActionName"> </MenuItem> </菜单> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> </DataGrid.Columns> </DataGrid>

<DataGrid.Columns> <DataGridTextColumn Binding="{Binding Product}" /> <DataGridTextColumn Binding="{Binding Version}" ></DataGridTextColumn> // Some other column like Activation Status, Expire Date etc... <DataGridTemplateColumn> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <Menu> <MenuItem Header="Choose Acions" ItemsSource="{Binding Path=DataContext.LicenseActions}" DisplayMemberPath="ActionName"> </MenuItem> </Menu> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> </DataGrid.Columns> </DataGrid>

DataGrid的项目来源是  ObservableCollection< Product> ;,我正在通过 ProductViewModel ,并且每个产品都包含

Item source for DataGrid is ObservableCollection<Product>, which I am passing through ProductViewModel and each product contain 

public class Product
{
  public string Product { get; set; }
  public string LicenseType { get; set; }
  public string Status { get; set; }
  public string Expires { get; set; }
  public List<LicenseAction> LicenseActions { get; set; }
}

每个产品的LicenseAction都不同.那么如何在WPF中使用MVVM将licenseAction与菜单控件绑定

LicenseAction is different for each product. so how to bind licenseActions with Menu control using MVVM in WPF

public class LicenseAction
    {
        public string ActionId { get; set; }
        public string ActionName { get; set; }
        public Visibility Visibility { get; set; }
        public bool IsEnabled { get; set; }
        public string ToolTip { get; set; }
    }
// possible license actions { Activate, Deactivate, View Details, View History }







推荐答案

我认为MenuItem的ItemsSource的绑定表达式不正确.  每个单元格都有一个Product对象作为DataContext.  因此,绑定应该只是{Binding LicenseActions}.
I think the binding expression for the ItemsSource of the MenuItem is incorrect.  Each cell has as a DataContext a Product object.  Therefore the binding should just be {Binding LicenseActions}.


这篇关于如何使用MVVM在WPF中使用MenuItem设置绑定数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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