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 如果想自定义模板


关于这五种具体使用方式以后再说。




原创粉丝点击