WPF,XAML Datagrid - 仅在最后一行启用上下文菜单
WPF, XAML Datagrid - Context Menu only in last row enabled
我必须制作一个上下文菜单,但它应该只在最后一行启用。在所有其他行中,它应该被禁用。我有 1 或 x 行。
<DataGrid.ContextMenu>
<ContextMenu>
<MenuItem Name="change_entry" Header="change entry"/>
</ContextMenu>
</DataGrid.ContextMenu>
您可以使用 IMultiValueConverter
将 ContextMenu.IsEnabled
属性 绑定到 DataGrid.SelectedIndex
和 DataGrid.Items.Count
属性。如果这些值中的任何一个发生变化,它将更新。这里是 XAML:
<Window x:Class="DataGridMenuTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:DataGridMenuTest"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<local:SelectedRowToBoolConverter x:Key="SelectedRowToBoolConverter"/>
</Window.Resources>
<Grid>
<DataGrid Name="MainGrid">
<DataGrid.RowStyle>
<Style TargetType="{x:Type DataGridRow}">
<Setter Property="ContextMenu">
<Setter.Value>
<ContextMenu>
<ContextMenu.IsEnabled>
<MultiBinding Mode="OneWay" Converter="{StaticResource SelectedRowToBoolConverter}">
<Binding ElementName="MainGrid" Path="SelectedIndex"/>
<Binding ElementName="MainGrid" Path="Items.Count"/>
</MultiBinding>
</ContextMenu.IsEnabled>
<MenuItem Name="change_entry" Header="change entry"/>
</ContextMenu>
</Setter.Value>
</Setter>
</Style>
</DataGrid.RowStyle>
</DataGrid>
</Grid>
</Window>
下面是转换器代码:
public class SelectedRowToBoolConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
if (values[0] == DependencyProperty.UnsetValue || values[1] == DependencyProperty.UnsetValue)
return false;
int selectedIndex = (int)values[0];
int rowsCount = (int)values[1];
return (selectedIndex == rowsCount - 1);
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
我必须制作一个上下文菜单,但它应该只在最后一行启用。在所有其他行中,它应该被禁用。我有 1 或 x 行。
<DataGrid.ContextMenu>
<ContextMenu>
<MenuItem Name="change_entry" Header="change entry"/>
</ContextMenu>
</DataGrid.ContextMenu>
您可以使用 IMultiValueConverter
将 ContextMenu.IsEnabled
属性 绑定到 DataGrid.SelectedIndex
和 DataGrid.Items.Count
属性。如果这些值中的任何一个发生变化,它将更新。这里是 XAML:
<Window x:Class="DataGridMenuTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:DataGridMenuTest"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<local:SelectedRowToBoolConverter x:Key="SelectedRowToBoolConverter"/>
</Window.Resources>
<Grid>
<DataGrid Name="MainGrid">
<DataGrid.RowStyle>
<Style TargetType="{x:Type DataGridRow}">
<Setter Property="ContextMenu">
<Setter.Value>
<ContextMenu>
<ContextMenu.IsEnabled>
<MultiBinding Mode="OneWay" Converter="{StaticResource SelectedRowToBoolConverter}">
<Binding ElementName="MainGrid" Path="SelectedIndex"/>
<Binding ElementName="MainGrid" Path="Items.Count"/>
</MultiBinding>
</ContextMenu.IsEnabled>
<MenuItem Name="change_entry" Header="change entry"/>
</ContextMenu>
</Setter.Value>
</Setter>
</Style>
</DataGrid.RowStyle>
</DataGrid>
</Grid>
</Window>
下面是转换器代码:
public class SelectedRowToBoolConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
if (values[0] == DependencyProperty.UnsetValue || values[1] == DependencyProperty.UnsetValue)
return false;
int selectedIndex = (int)values[0];
int rowsCount = (int)values[1];
return (selectedIndex == rowsCount - 1);
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}