如何正确设置Datagrid的ContextMenu的数据绑定(MVVM Light)

时间:2016-11-05 20:38:13

标签: c# xaml data-binding mvvm-light

有人可以看看我的XAML,请告诉我为什么我的MenuItem上的命令不起作用。我假设我的DataGrid的Tag绑定中有一个错误,但是,我似乎无法理解正在发生的事情。

在旁注中我理解上下文菜单不共享相同的可视树。我也理解如果我将ContextMenu DataContext设置为Window的那个,一切都按预期工作。我试图理解为什么我的PlacementTarget.Tag绑定不能按预期工作。提前谢谢。

视图如下:

<Window x:Class="WpfApplication8.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication8"
        xmlns:diag="clr-namespace:System.Diagnostics;assembly=WindowsBase"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525"
        DataContext="{Binding Source={StaticResource Locator}, Path=Main}">
    <Grid>
        <DataGrid ItemsSource="{Binding People}" 
                  AutoGenerateColumns="False" 
                  Tag="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}">
            <DataGrid.Resources>
                <ContextMenu x:Key="Context"
                            DataContext="{Binding PlacementTarget.Tag, RelativeSource={RelativeSource Self}}">
                    <MenuItem Header="Open" Command="{Binding OpenCommand, diag:PresentationTraceSources.TraceLevel=High}"/>
                </ContextMenu>
            </DataGrid.Resources>
            <DataGrid.Columns>
                <DataGridTextColumn Header="First" Binding="{Binding FirstName}"/>
            </DataGrid.Columns>
            <DataGrid.RowStyle>
                <Style TargetType="{x:Type DataGridRow}">
                    <Setter Property="ContextMenu" Value="{StaticResource Context}"/>
                </Style>
            </DataGrid.RowStyle>
        </DataGrid>
    </Grid>
</Window>

viewmodel

public class MainViewModel : ViewModelBase
    {
        /// <summary>
        /// Initializes a new instance of the MainViewModel class.
        /// </summary>
        public MainViewModel()
        {
            if (IsInDesignMode)
            {
                // Code runs in Blend --> create design time data.
            }
            else
            {
                // Code runs "for real"
                People = new ObservableCollection<Person>
                {
                    new Person { FirstName = "Tim",  },
                    new Person { FirstName = "Todd" },
                    new Person { FirstName = "Jane" },
                    new Person { FirstName = "Doe" },
                };
            }
        }

        public ObservableCollection<Person> People { get; set; }

        private RelayCommand _openCommand;

        /// <summary>
        /// Gets the OpenCommand.
        /// </summary>
        public RelayCommand OpenCommand
        {
            get
            {
                return _openCommand
                    ?? (_openCommand = new RelayCommand(
                    () =>
                    {
                        Debug.WriteLine("YES!");
                    }));
            }
        }
    }

和模型

public class Person
    {
        public string FirstName { get; set; }

    }

和一些调试信息

System.Windows.Data Warning: 56 : Created BindingExpression (hash=18243409) for Binding (hash=24719867)
System.Windows.Data Warning: 58 :   Path: 'OpenCommand'
System.Windows.Data Warning: 60 : BindingExpression (hash=18243409): Default mode resolved to OneWay
System.Windows.Data Warning: 61 : BindingExpression (hash=18243409): Default update trigger resolved to PropertyChanged
System.Windows.Data Warning: 62 : BindingExpression (hash=18243409): Attach to System.Windows.Controls.MenuItem.Command (hash=668104)
System.Windows.Data Warning: 67 : BindingExpression (hash=18243409): Resolving source 
System.Windows.Data Warning: 70 : BindingExpression (hash=18243409): Found data context element: MenuItem (hash=668104) (OK)
System.Windows.Data Warning: 71 : BindingExpression (hash=18243409): DataContext is null
System.Windows.Data Warning: 65 : BindingExpression (hash=18243409): Resolve source deferred
System.Windows.Data Warning: 67 : BindingExpression (hash=18243409): Resolving source 
System.Windows.Data Warning: 70 : BindingExpression (hash=18243409): Found data context element: MenuItem (hash=668104) (OK)
System.Windows.Data Warning: 71 : BindingExpression (hash=18243409): DataContext is null
System.Windows.Data Warning: 67 : BindingExpression (hash=18243409): Resolving source 
System.Windows.Data Warning: 70 : BindingExpression (hash=18243409): Found data context element: MenuItem (hash=668104) (OK)
System.Windows.Data Warning: 71 : BindingExpression (hash=18243409): DataContext is null
System.Windows.Data Warning: 67 : BindingExpression (hash=18243409): Resolving source 
System.Windows.Data Warning: 70 : BindingExpression (hash=18243409): Found data context element: MenuItem (hash=668104) (OK)
System.Windows.Data Warning: 71 : BindingExpression (hash=18243409): DataContext is null
System.Windows.Data Warning: 67 : BindingExpression (hash=18243409): Resolving source 
System.Windows.Data Warning: 70 : BindingExpression (hash=18243409): Found data context element: MenuItem (hash=668104) (OK)
System.Windows.Data Warning: 71 : BindingExpression (hash=18243409): DataContext is null
System.Windows.Data Warning: 67 : BindingExpression (hash=18243409): Resolving source  (last chance)
System.Windows.Data Warning: 70 : BindingExpression (hash=18243409): Found data context element: MenuItem (hash=668104) (OK)
System.Windows.Data Warning: 78 : BindingExpression (hash=18243409): Activate with root item <null>
System.Windows.Data Warning: 106 : BindingExpression (hash=18243409):   Item at level 0 is null - no accessor
System.Windows.Data Warning: 80 : BindingExpression (hash=18243409): TransferValue - got raw value {DependencyProperty.UnsetValue}
System.Windows.Data Warning: 88 : BindingExpression (hash=18243409): TransferValue - using fallback/default value <null>
System.Windows.Data Warning: 89 : BindingExpression (hash=18243409): TransferValue - using final value <null>

0 个答案:

没有答案