UWP DataGrid后台创建Column和绑定

DataGrid是一个好东西,绑定数据非常简单,而且数据显示的也非常完美。一般一开始学习的时候都是在XMAL里面设定好列与数据绑定的。但是很多时候我们需要在后台动态创建列,并为列绑定我们需要数据。

这里我就给个例子,大家看一下。例子稍微有点复杂,加入了复杂属性绑定的演示。其实动态创建列的绑定主要就是要知道用 Binding.PropertyPath 来设置。

dgtc.Binding = new Binding() { Path = new PropertyPath("cName") };

<controls:DataGrid Name="MyDataGrid" Grid.Row="1" AutoGenerateColumns="False" 
                           Loaded="MyDataGrid_Loaded"
                          >
            <controls:DataGrid.Columns >
                <!-- this is a fixed column to demonstrate TemplateColumn -->
                <controls:DataGridTemplateColumn Header="Template">
                    <controls:DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock SizeChanged="TextBlock_SizeChanged" HorizontalAlignment="Stretch" Text="{Binding cName}"/>
                        </DataTemplate>
                    </controls:DataGridTemplateColumn.CellTemplate>
                </controls:DataGridTemplateColumn>
                <!-- There will be 3 TextColumns added below in C# -->
            </controls:DataGrid.Columns >
        </controls:DataGrid>
    //Class definition
    public class StatisticalData
    {
        public double Mean { get; set; }
        public double Stdev { get; set; }
        public double Cpk { get; set; }
    }
    public class DataModel
    {
        public string cName { get; set; }
        public List<int> cNums { get; set; }
        public List<StatisticalData> Statisticals { get; set; }
    }

    //Main page
    public sealed partial class MainPage : Page
    {
        List<DataModel> dats = new List<DataModel>();

        private void MyDataGrid_Loaded(object sender, RoutedEventArgs e)
        {
            //Add DataGridTextColumn
            DataGridTextColumn dgtc = new DataGridTextColumn();
            dgtc.Header = "Name"; dgtc.IsReadOnly = true;
            //Setup Column Binding using PropertyPath
            dgtc.Binding = new Binding() { Path = new PropertyPath("cName") };
            MyDataGrid.Columns.Add(dgtc);
 

            //Other columns
            MyDataGrid.Columns.Add(new DataGridTextColumn()
            {
                Header = "Nums",
                IsReadOnly = true,
                Binding = new Binding() { Path = new PropertyPath("cNums[0]") }
            });
            MyDataGrid.Columns.Add(new DataGridTextColumn()
            {
                Header = "Mean",
                IsReadOnly = true,
                Binding = new Binding() { Path = new PropertyPath("Statisticals[2].Cpk") }
            });

            //Bind Data
            dats.Add(new DataModel() { cName = "Hiyina38fdz8sfefi", cNums = new List<int> { 1, 2, 3 } });
            dats.Add(new DataModel() { cName = "Mamuwa", cNums = new List<int> { 4, 5, 6 } });
            List<StatisticalData> StaList = new List<StatisticalData>();
            StaList.Add(new StatisticalData() { Mean = 71, Stdev = 0.03, Cpk = 1.45 });
            StaList.Add(new StatisticalData() { Mean = 72, Stdev = 0.04, Cpk = 1.465 });
            StaList.Add(new StatisticalData() { Mean = 73, Stdev = 0.05, Cpk = 1.475 });
            dats[0].Statisticals = StaList;
            dats[1].Statisticals = StaList.Select(p => p).ToList();
            //Setup DataGrid.ItemSource
            MyDataGrid.ItemsSource = dats;

        }

    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值