WPF DataGrid 控件
默认
C# 代码
public partial class DataGridView : Window { public DataGridViewModel viewModel; public DataGridView() { InitializeComponent(); this .viewModel = new DataGridViewModel(); this .viewModel.personList.Add( new Person { Id = 1, Name = "马云" }); this .viewModel.personList.Add( new Person { Id = 2, Name = "刘强东" }); this .viewModel.personList.Add( new Person { Id = 3, Name = "曹德旺" }); this .DataContext = this .viewModel; } } public class Person { public int Id { get ; set ; } public string Name { get ; set ; } } public class DataGridViewModel : INotifyPropertyChanged { public ObservableCollection<Person> personList { set ; get ; } public DataGridViewModel() { this .personList = new ObservableCollection<Person>(); } public event PropertyChangedEventHandler PropertyChanged; public void RaisePropertyChanged( string propertyName) { if ( this .PropertyChanged != null ) { this .PropertyChanged( this , new PropertyChangedEventArgs(propertyName)); } } } |
XAML 代码
<DataGrid ItemsSource= "{Binding personList, Mode=TwoWay}" /> |
视图效果
1、DataGrid 加上 RowHeaderWidth="0" 取消最左面的一列
2、CanUserAddRows="False" 表示不让DataGrid自动生成行
自动添加行号:
C# 代码
private void dataGrid_LoadingRow( object sender, DataGridRowEventArgs e) { e.Row.Header = e.Row.GetIndex() + 1; } |
XAML 代码
< DataGrid x:Name="dataGrid" ItemsSource="{Binding personList, Mode=TwoWay}" LoadingRow="dataGrid_LoadingRow"/> |
视图效果
按 Enter 键焦点进入下一个单元格
C# 代码
private void dataGrid_PreviewKeyDown( object sender, KeyEventArgs e) { var uie = e.OriginalSource as UIElement; if (e.Key == Key.Enter) { uie.MoveFocus( new TraversalRequest(FocusNavigationDirection.Next)); e.Handled = true ; } } |
XAML 代码
< DataGrid x:Name="dataGrid" ItemsSource="{Binding personList, Mode=TwoWay}" PreviewKeyDown="dataGrid_PreviewKeyDown"/> |
取得当前焦点所在的行
C# 代码
private void Button_Click( object sender, RoutedEventArgs e) { var _cells = dataGrid.SelectedCells; if (_cells.Any()) { string columnName = dataGrid.CurrentColumn.Header.ToString(); int rowIndex = dataGrid.Items.IndexOf(_cells.First().Item); } } |
行焦点改变时,获取所有的 cell 值
C# 代码
private void dataGrid_SelectedCellsChanged( object sender, SelectedCellsChangedEventArgs e) { var cells = dataGrid.SelectedCells; if (cells.Any()) { int rowIndex = dataGrid.Items.IndexOf(cells.First().Item); foreach ( var cell in cells) { string Text = (cell.Column.GetCellContent(cell.Item) as TextBlock).Text; } } } |
XAML 代码
1 | < DataGrid x:Name="dataGrid" ItemsSource="{Binding personList, Mode=TwoWay}" SelectedCellsChanged="dataGrid_SelectedCellsChanged"/> |
绑定MVVM行命令
< DataGridTemplateColumn Header="操作" Width="*"> < DataGridTemplateColumn.CellTemplate > < DataTemplate > < StackPanel Orientation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="Center"> < Button Content="更新" Tag="{Binding UpgraderName}"> < i:Interaction.Triggers > < i:EventTrigger EventName="Click"> < i:InvokeCommandAction CommandParameter="{Binding}" Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.CommandSave}" /> </ i:EventTrigger > </ i:Interaction.Triggers > </ Button > < Button Content="删除" Tag="{Binding UpgraderName}"> < i:Interaction.Triggers > < i:EventTrigger EventName="Click"> < i:InvokeCommandAction CommandParameter="{Binding}" Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.CommandDelete}" /> </ i:EventTrigger > </ i:Interaction.Triggers > </ Button > </ StackPanel > </ DataTemplate > </ DataGridTemplateColumn.CellTemplate > </ DataGridTemplateColumn > |
C# 命令
/// <summary> /// 删除 /// </summary> private DelegateCommand<UpgraderModel> _commandDelete = null ; public DelegateCommand<UpgraderModel> CommandDelete { get { if (_commandDelete == null ) { _commandDelete = new DelegateCommand<UpgraderModel>( new Action<UpgraderModel>((model) => { //你需要执行的代码 })); } return _commandDelete; } } |
隐藏DataGrid最后一行多一个空白行的解决方法
1.产生的原因:该行是为了让用户输入信息到里边预留的空白行。所以即使填充的数据没内容一样会出现空白行,
2.解决方法:将DataGrid的属性CanUserAddRows属性改为False即可
1 | <DataGrid CanUserAddRows= "False" ></DataGrid> |
隐藏DataGrid自动产生默认列名的解决方法
1.解决方法:将DataGrid的属性AutoGenerateColumns属性改为False即可,如:
<DataGrid AutoGenerateColumns= "False" ></DataGrid> |
隐藏DataGrid最前面一列空白不显示的解决方法
1.解决方法:将DataGrid的属性RowHeaderWidth属性改为0即可,如:
<DataGrid RowHeaderWidth= "0" ></DataGrid> |
DataGrid 禁止表头排序
1.解决方法:将DataGrid的属性CanUserSortColumns属性改为False即可,如:
< DataGrid CanUserSortColumns="False"></ DataGrid > |
WPF Datagrid删除一行
https://www.cnblogs.com/ZXdeveloper/p/3904314.html