上下文菜单项命令使用MVVM绑定WPF [英] Context Menu items command binding WPF using MVVM

查看:178
本文介绍了上下文菜单项命令使用MVVM绑定WPF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个问题在许多网站和StackOverFlow中以不同的方式被问到很多次,但是我发现的所有答案并不能帮助我确切地说,我无法理解它们并在我的应用程序中实现。所以我想从我的应用程序中添加一些代码,以便您能帮助我更好。

I know this question has been asked many times in different ways in many websites and also in StackOverFlow but all the answers I found are not helping me ot to be precise I am not able to understand them and implement in my application. So I thought of putting some code from my application so that you people can help me better.

问题陈述:我正在使用WPF DataGrid。我添加了一个上下文菜单,我有3个选项剪切,复制,粘贴。我正在使用MVVM进行开发。我想要DataBind这些选项到我的ViewModel中的命令。但我无法做同样的事情。上下文菜单选项根本没有获取数据绑定!

Problem Statement : I am using a WPF DataGrid. I have added a Context Menu and I have 3 options Cut,Copy,Paste. I am using MVVM for development. I want to DataBind these options to Commands in my ViewModel. But I am unable to do the same. The context menu options are not getting data bound at all !!!

这是XAML中的网格代码:

This is my Grid Code in XAML :

<custom:DataGrid  
      x:Name="DataGrid_Standard"   
      Grid.Row="1" Grid.Column="1"   
      AutoGenerateColumns="False"                                                           
      IsSynchronizedWithCurrentItem="True"   
      Background="Transparent"
      ItemsSource="{Binding FullGridData}" 
      ItemContainerStyle="{StaticResource defaultRowStyle}"
      ColumnHeaderStyle="{StaticResource DefaultColumnHeaderStyle}"                         
      Grid.ColumnSpan="2">

然后我有一个ContextMenu和一个标题元素的样式

Then I have a ContextMenu and a Style for Header Element

<ContextMenu x:Key="columnHeaderMenu">
   <MenuItem Command="{Binding CutCommand}"
             Header="Test" />
   <MenuItem Header="Copy"/>
   <MenuItem Header="Paste"/>
</ContextMenu>
<Style TargetType="{x:Type custom:DataGridColumnHeader}" x:Key="DefaultColumnHeaderStyle">
    <Setter Property="ContextMenu" Value="{DynamicResource columnHeaderMenu}" >
</Style>

此行在我的构造函数中

public Window1()
{            
   this.DataContext = new AppData();
}

此代码放在我的AppData类中:

This code goes in my AppData class:

public class AppData  
{ 
    private IList<GridData> fullGridData = new ObservableCollection<GridData>();<br> 
    public IList<GridData> FullGridData
    {
        get { return fullGridData; }
        set { fullGridData = value; }
    }

    private DelegateCommand<object> cutCommand;
    public DelegateCommand<object> CutCommand
    {
        get
        {
            if (cutCommand == null)
            {
                cutCommand = new DelegateCommand<object>(CutColumn);
            }
            return cutCommand;
        }
    }

    private void CutColumn(object obj)
    {
        //some code goes here
    }   
}

**我想知道我在哪里做错了?为什么DataBinding不会发生?
请帮助我。请提供我现在的代码中的示例代码或修改,我可以从中实现。 **

** I want to know exactly where am I doing wrong?? Why is the DataBinding not happening?? Please help me regarding this. Please provide me the sample code or modification in my present code from which I can achieve it. **

推荐答案

您的代码中的某些东西(或当时使用的WPF版本(?))是否过于复杂。我可以绑定如

Something in your code (or the version of WPF being used at the time(?)) is overcomplicating things. I am able to bind such as

<DataGrid AutoGenerateColumns="True"
        Name="myGrid"
        ItemsSource="{Binding Orders}">
    <DataGrid.ContextMenu>
        <ContextMenu>
            <MenuItem Header="Copy" Command="{Binding CopyItem}" />
            <MenuItem Header="Delete" Command="{Binding DeleteItem}" />
        </ContextMenu>
    </DataGrid.ContextMenu>
</DataGrid>

命令的设置如下:

VM.DeleteItem 
     = new OperationCommand((o) => MessageBox.Show("Delete Me"),
                            (o) => (myGrid.SelectedItem as Order)?.InProgress == false );

这篇关于上下文菜单项命令使用MVVM绑定WPF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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