网格中的空列,RowCommand 中的 selecteditem null
Empty column in grid, selecteditem null in RowCommand
我有一个数据网格,一些列显示为空白,来自代码隐藏的空值。右键单击列,我们通过获取所选行来执行操作。万一我们在列中有空值,然后右键单击,rowcommand 中的行是空白的。
知道吗,如何在 selecteditem 为 null 时仍然获取行?
XAML :
<DataGrid>
<DataGrid.Columns>
<DataGridTextColumn></DataGridTextColumn>
</DataGrid.Columns>
<DataGrid.ContextMenu>
<ContextMenu>
<MenuItem Header="Edit" ToolTip="Edit the inputs."
Command="{Binding DataContext.MenuEditRowCommand,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=ContextMenu}}"
CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}, Path=PlacementTarget.SelectedItem}">
</MenuItem>
</ContextMenu>
</DataGrid.ContextMenu>
</DataGrid>
视图模型:
public ICommand MenuEditRowCommand
{
get
{
if (_menuEditRowCommand == null)
{
_menuEditRowCommand = new ExceptionHandlingDelegateCommand<Class_NAme>(MenuEditRowMethod);
}
return _menuEditRowCommand;
}
}
private void MenuEditRowMethod(Class_NAme Inputs)
{
if (Inputs != null)
{
//
}
}
为 DataGridRow
容器定义一个 ContextMenu
并绑定到它的 DataContext
:
<DataGrid>
<DataGrid.Columns>
<DataGridTextColumn></DataGridTextColumn>
</DataGrid.Columns>
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Setter Property="Tag" Value="{Binding Path=DataContext.MenuEditRowCommand, RelativeSource={RelativeSource AncestorType=DataGrid}}" />
<Setter Property="ContextMenu">
<Setter.Value>
<ContextMenu>
<MenuItem Header="Edit" ToolTip="Edit the inputs."
Command="{Binding Path=PlacementTarget.Tag, RelativeSource={RelativeSource AncestorType=ContextMenu}}"
CommandParameter="{Binding Path=PlacementTarget.DataContext, RelativeSource={RelativeSource AncestorType=ContextMenu}}">
</MenuItem>
</ContextMenu>
</Setter.Value>
</Setter>
</Style>
</DataGrid.RowStyle>
</DataGrid>
我有一个数据网格,一些列显示为空白,来自代码隐藏的空值。右键单击列,我们通过获取所选行来执行操作。万一我们在列中有空值,然后右键单击,rowcommand 中的行是空白的。
知道吗,如何在 selecteditem 为 null 时仍然获取行?
XAML :
<DataGrid>
<DataGrid.Columns>
<DataGridTextColumn></DataGridTextColumn>
</DataGrid.Columns>
<DataGrid.ContextMenu>
<ContextMenu>
<MenuItem Header="Edit" ToolTip="Edit the inputs."
Command="{Binding DataContext.MenuEditRowCommand,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=ContextMenu}}"
CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}, Path=PlacementTarget.SelectedItem}">
</MenuItem>
</ContextMenu>
</DataGrid.ContextMenu>
</DataGrid>
视图模型:
public ICommand MenuEditRowCommand
{
get
{
if (_menuEditRowCommand == null)
{
_menuEditRowCommand = new ExceptionHandlingDelegateCommand<Class_NAme>(MenuEditRowMethod);
}
return _menuEditRowCommand;
}
}
private void MenuEditRowMethod(Class_NAme Inputs)
{
if (Inputs != null)
{
//
}
}
为 DataGridRow
容器定义一个 ContextMenu
并绑定到它的 DataContext
:
<DataGrid>
<DataGrid.Columns>
<DataGridTextColumn></DataGridTextColumn>
</DataGrid.Columns>
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Setter Property="Tag" Value="{Binding Path=DataContext.MenuEditRowCommand, RelativeSource={RelativeSource AncestorType=DataGrid}}" />
<Setter Property="ContextMenu">
<Setter.Value>
<ContextMenu>
<MenuItem Header="Edit" ToolTip="Edit the inputs."
Command="{Binding Path=PlacementTarget.Tag, RelativeSource={RelativeSource AncestorType=ContextMenu}}"
CommandParameter="{Binding Path=PlacementTarget.DataContext, RelativeSource={RelativeSource AncestorType=ContextMenu}}">
</MenuItem>
</ContextMenu>
</Setter.Value>
</Setter>
</Style>
</DataGrid.RowStyle>
</DataGrid>