为WPF 4 DataGrid的列标题添加右键单击上下文菜单,可以通过以下步骤实现:
<DataGrid x:Name="dataGrid" AutoGenerateColumns="False" ItemsSource="{Binding DataList}">
<DataGrid.Resources>
<ContextMenu x:Key="ColumnHeaderContextMenu">
<MenuItem Header="重命名" Command="{Binding ColumnRenameCommand}" />
<MenuItem Header="排序" Command="{Binding ColumnSortCommand}" />
<MenuItem Header="隐藏" Command="{Binding ColumnHideCommand}" />
</ContextMenu>
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTextColumn Header="ID" Binding="{Binding ID}" />
<DataGridTextColumn Header="名称" Binding="{Binding Name}" />
<DataGridTextColumn Header="描述" Binding="{Binding Description}" />
</DataGrid.Columns>
</DataGrid>
public MainWindow()
{
InitializeComponent();
dataGrid.ColumnHeaderContextMenu = (ContextMenu)dataGrid.Resources["ColumnHeaderContextMenu"];
}
public class MainViewModel : INotifyPropertyChanged
{
public ICommand ColumnRenameCommand { get; private set; }
public ICommand ColumnSortCommand { get; private set; }
public ICommand ColumnHideCommand { get; private set; }
public MainViewModel()
{
ColumnRenameCommand = new RelayCommand(ColumnRename);
ColumnSortCommand = new RelayCommand(ColumnSort);
ColumnHideCommand = new RelayCommand(ColumnHide);
}
private void ColumnRename()
{
// 重命名列的逻辑
}
private void ColumnSort()
{
// 排序列的逻辑
}
private void ColumnHide()
{
// 隐藏列的逻辑
}
// 其他代码
}
private void ColumnRename()
{
// 重命名列的逻辑
}
private void ColumnSort()
{
// 排序列的逻辑
}
private void ColumnHide()
{
// 隐藏列的逻辑
}
通过以上步骤,可以为WPF 4 DataGrid的列标题添加右键单击上下文菜单,并实现重命名列、排序列和隐藏列的功能。
没有搜到相关的沙龙
领取专属 10元无门槛券
手把手带您无忧上云