Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf DataGrid行上的ContextMenu命令不起作用_Wpf_Mvvm_Datagrid_Row - Fatal编程技术网

Wpf DataGrid行上的ContextMenu命令不起作用

Wpf DataGrid行上的ContextMenu命令不起作用,wpf,mvvm,datagrid,row,Wpf,Mvvm,Datagrid,Row,我创建一个样式,然后将其绑定到一个DataGrid.RowStyleRow样式。出现ContextMenu,我可以从菜单项中进行选择,但是命令将不会执行。我用其他控件测试了命令,效果良好 <Style x:Key="DataGridRow" TargetType="{x:Type DataGridRow}"> <Setter Property="ContextMenu"> <Setter.Value>

我创建一个样式,然后将其绑定到一个
DataGrid.RowStyle
Row
样式。出现
ContextMenu
,我可以从菜单项中进行选择,但是命令将不会执行。我用其他控件测试了命令,效果良好

<Style x:Key="DataGridRow"  TargetType="{x:Type DataGridRow}">
     <Setter Property="ContextMenu">
         <Setter.Value>
              <ContextMenu DataContext="{Binding PlacementTarget.DataContext, RelativeSource={RelativeSource Self}}">
                    <MenuItem Header="Add" Command="{Binding AddMessageContextMenu_Command}"/>
                    <MenuItem Header="Edit" Command="{Binding EditMessage_Command}"/>
              </ContextMenu>
           </Setter.Value>
     </Setter>
<Style>

<DataGrid Name="SearchTableDataGrid" ItemsSource="{Binding SearchTableDataGrid_ItemSource}"
   <DataGrid.RowStyle>
     <Style TargetType="{x:Type DataGridRow}">
          <Style.Triggers>
             <Trigger Property="AlternationIndex" Value="0">
                   <Setter Property="Background" Value="#FBFCFC" />
              </Trigger>
              <Trigger Property="AlternationIndex" Value="1">
                   <Setter Property="Background" Value="#f6f8f8" />
              </Trigger>
          </Style.Triggers>
      </Style>
   </DataGrid.RowStyle>
</DataGrid>


您需要对正确的DataContext进行引用。正如注释中所建议的,您可以为此使用Rows
标记
属性

下面是获取Windows DataContext的代码

<Style TargetType="{x:Type DataGridRow}">
    <Setter Property="Tag" 
            Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}}, 
                            Path=DataContext}"/>
    <Setter Property="ContextMenu">
        <Setter.Value>
            <ContextMenu DataContext="{Binding PlacementTarget.Tag, 
                                               RelativeSource={RelativeSource Self}}">
                <MenuItem Header="Add" Command="{Binding AddMessageContextMenu_Command}"/>
                <MenuItem Header="Edit" Command="{Binding EditMessage_Command}"/>
            </ContextMenu>
        </Setter.Value>
    </Setter>
</Style>


Related link:我使用了标记,但命令仍然无法执行。您需要在运行时使用visual studio 2015中的工具或类似Snoop的工具检查绑定。谢谢,我声明了一个不同的RelativeSource。现在它绑定到正确的命令。