WPF DataGrid 使用
1、举例
<Window x:Class="WpfApplication1.com.view.DataGridWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"Title="DataGridWindow" Height="300" Width="500"><Grid><DataGrid Name="datagrid1" AutoGenerateColumns="True"/><StackPanel HorizontalAlignment="Right" VerticalAlignment="Top"><Button Name="btnShowData" Content="显示数据" Click="btnShowData_Click"/></StackPanel></Grid>
</Window>
namespace WpfApplication1.com.view
{/// <summary>/// DataGridWindow.xaml 的交互逻辑/// </summary>public partial class DataGridWindow : Window{private List<StudentData> studentDatas = new List<StudentData>();public DataGridWindow(){InitializeComponent();for(int i=1;i<=5;i++){StudentData stuData = new StudentData();stuData.studentID = 100 + i;stuData.studentName = "姓名" + i;studentDatas.Add(stuData);}}private void btnShowData_Click(object sender, RoutedEventArgs e){datagrid1.ItemsSource = studentDatas;}}
}
namespace WpfApplication1.com.Data
{public class StudentData : BindableObject{public int studentID = 0;public string studentName = "";public int StudentID{get { return studentID; }set { SetProperty<int>(ref studentID, value); }}public string StudentName{get { return studentName; }set { SetProperty<string>(ref studentName, value); }}}
}
namespace WpfApplication1.com.Data
{public abstract class BindableObject : INotifyPropertyChanged{public event PropertyChangedEventHandler PropertyChanged;protected void OnPropertyChanged(string propertyName){if (PropertyChanged != null){PropertyChanged(this, new PropertyChangedEventArgs(propertyName));}}protected void SetProperty<T>(ref T item, T value, [CallerMemberName] string propertyName = null){if (!EqualityComparer<T>.Default.Equals(item, value)){item = value;OnPropertyChanged(propertyName);}}}
}
以上是一个最简单的例子:
AutoGenerateColumns属性:是否自动创建列(默认为true)
CanUserAddRows属性:是否可以添加新行
需要注意的是数据源,这个具体如何提供数据方式很多种,需要绑定,并时时更新需要实现INotifyPropertyChanged接口
2、举例
<Window x:Class="WpfApplication1.com.view.DataGridWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"Title="DataGridWindow" Height="300" Width="500"><Grid><DataGrid Name="datagrid1" AutoGenerateColumns="True" Visibility="Hidden"/><DataGrid Name="datagrid2" AutoGenerateColumns="False" CanUserAddRows="False"><DataGrid.Columns><DataGridTextColumn Binding="{Binding StudentID}" Header="学号"/><DataGridTextColumn Binding="{Binding StudentName}" Header="姓名"/></DataGrid.Columns></DataGrid><StackPanel HorizontalAlignment="Right" VerticalAlignment="Top"><Button Name="btnShowData" Content="显示数据" Click="btnShowData_Click"/></StackPanel></Grid>
</Window>
这个datagrid2是指定了显示哪些数据,并设置了列标题
CanUserAddRow属性:是否可以添加新行(默认为True),从例1效果图看看到每次生成的表格最下面都会有一个空白行,如果设置为false,则不会显示空白行
3、关于DataGrid的列的呈现方式
(1)DataGridTextColumn 文本
(2)DataGridCheckBoxColumn 复选框
(3)DataGridComboBoxColumn 下拉菜单
(4)DataGridHyperlinkColumn 超链接文本
(5)DataGridTemplateColumn 如果想自定义模板
3、举例
有一个表头有个复选框可以进行全选
数据类
public abstract class BindableObject : INotifyPropertyChanged{public event PropertyChangedEventHandler PropertyChanged;protected void OnPropertyChanged(string propertyName){if (PropertyChanged != null){PropertyChanged(this, new PropertyChangedEventArgs(propertyName));}}protected void SetProperty<T>(ref T item, T value, [CallerMemberName] string propertyName = null){if (!EqualityComparer<T>.Default.Equals(item, value)){item = value;OnPropertyChanged(propertyName);}}}
public class Person : BindableObject{private string _name = "";private bool _isSelected = false;public string Name{get { return _name; }set { SetProperty<string>(ref _name, value); }}public bool IsSelected{get { return _isSelected; }set { SetProperty<bool>(ref _isSelected, value); }}}
前台XAML
需要注意<CheckBox IsChecked="{Binding IsSelected,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>进行了双向绑定
<Window x:Class="WpfApplication1.DataGridWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"Title="DataGridWindow" Height="500" Width="800"><Grid><DataGrid Name="datagrid1" AutoGenerateColumns="False"><DataGrid.Columns><DataGridTemplateColumn><DataGridTemplateColumn.HeaderTemplate><DataTemplate><CheckBox x:Name="checkAll" Click="checkAll_Click"/></DataTemplate></DataGridTemplateColumn.HeaderTemplate><DataGridTemplateColumn.CellTemplate><DataTemplate><CheckBox IsChecked="{Binding IsSelected,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/></DataTemplate></DataGridTemplateColumn.CellTemplate></DataGridTemplateColumn><DataGridTextColumn Binding="{Binding Name}" Header="姓名"/></DataGrid.Columns></DataGrid></Grid>
</Window>
public partial class DataGridWindow : Window{private List<Person> _personList = new List<Person>();public DataGridWindow(){InitializeComponent();_personList.Add(new Person { Name="小明"});_personList.Add(new Person { Name = "小红"});datagrid1.ItemsSource = _personList;}private void checkAll_Click(object sender, RoutedEventArgs e){for(int i=0;i<_personList.Count;i++){_personList[i].IsSelected = !_personList[i].IsSelected;}}}
行数据、列数据、单元数据获取方式
//获取行
int rowIndex = 0;
DataGridRow gridRow = (DataGridRow)datagrid1.ItemContainerGenerator.ContainerFromIndex(rowIndex);//获取列
int columnIndex = 0;
DataGridColumn gridColumn = datagrid1.Columns[columnIndex];//获取单元格数据
FrameworkElement element = gridColumn.GetCellContent(gridRow);
获取第一列的DataGridCheckBoxColumn方法
前台
<DataGrid Name="datagrid1" AutoGenerateColumns="False" CanUserAddRows="False" Width="350" Height="200" LoadingRow="datagrid1_LoadingRow"><DataGrid.Columns><DataGridCheckBoxColumn/><DataGridTextColumn Binding="{Binding ScreenX}" Header="ScreenX"/><DataGridTextColumn Binding="{Binding ScreenY}" Header="ScreenY"/><DataGridTextColumn Binding="{Binding R}" Header="R"/><DataGridTextColumn Binding="{Binding G}" Header="G"/><DataGridTextColumn Binding="{Binding B}" Header="B"/></DataGrid.Columns></DataGrid>
后台
//获取第1行第1列的这个就是DataGridCheckBoxColumnint rowIndex = 0;int columnIndex = 0;DataGridRow gridRow = (DataGridRow)datagrid1.ItemContainerGenerator.ContainerFromIndex(rowIndex);DataGridColumn gridColumn = datagrid1.Columns[columnIndex];FrameworkElement element = gridColumn.GetCellContent(gridRow);CheckBox checkBox = (System.Windows.Controls.CheckBox)element;//这个就是DataGridCheckBoxColumn
WPF DataGrid 使用相关推荐
- WPF Datagrid with some read-only rows - Stack Overflow
原文:WPF Datagrid with some read-only rows - Stack Overflow up vote 21 down vote accepted I had the sa ...
- WPF DataGrid 如何将被选中行带到视野中
WPF DataGrid 如何将被选中行带到视野中 目录 前言 准备工作 方法一 方法二 总结 独立观察员 2021 年 12 月 11 日 前言 在 WPF 开发中,显示表格一般使用 DataGri ...
- WPF DataGrid 通过自定义表头模拟首行固定
WPF DataGrid 通过自定义表头模拟首行固定 独立观察员 2021 年 9 月 25 日 最近工作中要在 WPF 中做个表格,自然首选就是 DataGrid 控件了.问题是,UI 设计的表格是 ...
- WPF DataGrid:解决排序、ScrollIntoView、刷新和焦点问题
目录 介绍 第一种方法:记住选定的行,刷新DataGrid,再次选择行 最终方法:使用OneWay绑定,避免调用Refresh() 改进1:使ScrollIntoView()起作用 改进2:将选定的行 ...
- 使用绑定进行WPF DataGrid格式化的指南
目录 介绍 WPF DataGrid结构 WPF绑定基础 使用的业务数据 将DataGrid与业务数据连接 DataGrid格式 格式化列 格式化完整行 根据显示的值格式化单元格 根据业务逻辑数据格式 ...
- WPF DataGrid 和LINQ to SQL示例程序之一 (提供源代码下载)
WPF DataGrid 和LINQ to SQL示例程序之一 (提供源代码下载) WPF DataGrid 系列示例程序,由浅入深逐步介绍如何在WPF 应用程序中使用新的DataGrid 控件.本篇 ...
- WPF DataGrid 主从表 数据绑定方式
昨天在网上搜了一下午没有看到一个关于WPF DataGrid主从表数据绑定的示例,但是我坚信这个简单的功能肯定是支持的,经研究问题解决. 现把相关方法共享下,给现在还在郁闷的兄弟们一点参考.重点在于定 ...
- wpf DataGrid主从表,DataGrid嵌套DataGrid主从结构rowdetailtemplate实现,绑定DataTable数据源,使用Visual Studio 2017
wpf DataGrid主从表,DataGrid嵌套DataGrid主从结构rowdetailtemplate实现,绑定DataTable数据源,使用Visual Studio 2017 . 子表绑定 ...
- wpf datagrid设置右键菜单打开时选中项的背景色
原文:wpf datagrid设置右键菜单打开时选中项的背景色 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/huangli321456/artic ...
- C# WPF DataGrid控件的详细介绍和推荐一些样式设计
前面介绍过使用DataGrid简单绑定一个数据模型,接着介绍DataGrid的一些详细操作. 参考:C# WPF DataGrid的使用 定制DataGrid控件基本外观属性 RowBackgroun ...
最新文章
- 火狐中H1到H5都有特定margn
- GitHub使用方法
- 第三次学JAVA再学不好就吃翔(part78)--List类
- String、StringBuffer和StringBuilde的区别
- 信息系统项目的应急预案方案_从环评到验收、排污许可证、应急预案,都应在项目什么阶段开展? 先后顺序是什么?...
- 【爬虫剑谱】二卷2章 实战篇-精美动漫图片爬取并保存
- asp.net验证空间详说
- OpenSessionInViewFilter失效问题
- 空字符串(“”)和null的区别
- 《疯狂的程序员》 -- 什么是真正的程序员?
- layui框架静态表格怎么写
- 几百万数据量的Excel导出会内存溢出和卡顿?那是你没用对方法!
- 扔垃圾前得先“刷脸”?北京这个小区垃圾分类真的用上了“人脸识别”!
- ESP32-C3 应用 篇(实例二、通过蓝牙将传感器数据发送给手机,手机端控制 SK6812 LED)
- A load persistent id instruction was encountered, but no persistent_load function was specified.
- java try catch 输入字符串_java – 没有在try / catch中捕获NumberFormatException
- idea智能提示设置和修改提示快捷键
- 家谱文化研究②:比赘婿更不光彩的家庭角色?何雨柱还乐在其中
- 盈通rx580游戏高手 bios_RX 5700 XT D6 游戏高手测评:女装大佬重捶出击!
- 感悟+北京and新疆知识点
热门文章
- jquery 获取鼠标点击的位置坐标
- 【李宏毅机器学习笔记】 23、循环神经网络(Recurrent Neural Network,RNN)
- 安卓木马demo_向安卓APK smali源码中植入木马
- Python实战之数据结构和算法
- unity 打印照片
- 尝试读取或写入受保护的内存。这通常指示其他内存已损坏
- 5月14日-----疯狂猜成语第二次站立会议-----杨霏,袁雪,胡潇丹,郭林林,尹亚男,赵静娜...
- java stringformat用法_string.format的用法 (java)
- 第一课 わたしは 田中(たなか)です
- Source Insight Win10上显示字体很模糊解决办法