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

查看:36
本文介绍了使用 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 进行开发.我想将这些选项数据绑定到我的 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>

这一行进入我的构造函数

This line goes in my constructor

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天全站免登陆