WPF DataGrid 使用
来源:互联网 发布:阿拉丁自有数据电解铝 编辑:程序博客网 时间:2024/05/23 23:43
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)
需要注意的是数据源,这个具体如何提供数据方式很多种,需要绑定,并时时更新需要实现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 如果想自定义模板
关于这五种具体使用方式以后再说。
阅读全文
0 0
- WPF DataGrid简介使用
- WPF DataGrid 使用
- WPF--DataGrid的使用
- WPF DataGrid DataGrid.Columns 使用笔记
- WPF学习之使用DataGrid
- WPF中DataGrid的使用
- MVVM WPF 使用 datagrid 绑定 comboBox
- wpf datagrid
- WPF DataGrid
- 使用wpf 的datagrid 对选择的数据操作
- WPF使用DataGrid控件显示和更新ADO.NET数据源
- WPF DataGrid使用 自动显示行号、全选、三级联动、拖拽
- WPF DataGrid使用 自动显示行号、全选、三级联动、拖拽
- WPF DataGrid使用 自动显示行号、全选、三级联动、拖拽
- WPF - 使用XML作为datagrid数据源进行binding
- WPF中DataGrid使用自定义列绑定数据
- WPF 之使用Treeview和DataGrid的关联绑定
- WPF使用MVVM完成DataGrid数据绑定带全选
- 搭建网站,和把大象装冰箱一样简单?
- ehcache初使用
- iOS AVAssetWriter 实现高分辨率录制视频,生成低体积的视频文件
- JNI c++对象与java对象互关联
- UEditor 图片上传
- WPF DataGrid 使用
- ActiveMQ的Hello World实例
- shiro springmvc
- mycat 启动和关闭服务不起作用解决方案
- ORA-32004 的错误处理
- centos 无法启动mysql问题
- hasOwnProperty() 、in 操作符与 for-in 循环
- Kafka
- TextView点击后EditText聚焦并显示软键盘