在wpf中动态地将列添加到DataGrid

在wpf中动态地将列添加到DataGrid

本文介绍了在wpf中动态地将列添加到DataGrid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在制作一个自定义画布,并且我必须添加一个表,所以我认为dataGrid会很好。所以我想从Datagrid创建表格,用户可以在运行时向画布添加表格。



到现在为止,我已经尝试使用列表填充DataGrid并成功。



如何在运行时将列添加到Datagrid,这样列数和标题值将在运行时使用文本框从用户处获取,并根据文本框的值,datagrid应添加列和标题值。



实际上我想开发一个表,其中用户传递了列数和列标题,并且应该生成表格。







你能否建议我使用DrawingVisual类来绘制表格

I am currently working on a custom canvas and in that i have to add a table,So i thought dataGrid would be fine. SO i Want to create a "Table" from "Datagrid" by which user can add a table to the canvas at runtime.

Till now, I have tried to Populate DataGrid With a list and succeded.

How Can I add Columns to a Datagrid at runtime,such that the number of columns and header value Would be taken from the user at runtime using a textbox and based on the value of the textbox the datagrid should add columns and header value.

Actually I want to develop a Table in which user passes the no of columns and the column header and the table should be generated.

Or

"Can you suggest me with a way where i should look in order to to "Draw" a Table using DrawingVisual class"

It is a part of GraphicsTable Class

//Custom Classes "DrawingCanvas & GraphicsTable" 
     public void CreateDataGrid(GraphicsTable graphicsTable, DrawingCanvas drawingCanvas)
        {
            dt = new DataGrid();
            dt.Name = "Data";
            dt.ItemsSource = person();
            dt.AllowDrop = true;
            dt.AutoGenerateColumns = true;
            dt.Height = graphicsTable.Rectangle.Height;
            dt.Width = graphicsTable.Rectangle.Width;
            drawingCanvas.Children.Add(dt);
            Canvas.SetTop(dt, graphicsTable.Rectangle.Top);
            Canvas.SetLeft(dt, graphicsTable.Rectangle.Left);
            dt.Width = dt.Width;
            dt.Height = dt.Height;
            dt.Focus();
        }
//I have just tried to add dome dummy data to the datagrid.

        public List person()
        {
            List peep = new List();
            peep.Add(new Person() {});
            return peep;
        }
    }

   public class Person
    {
        private string name;
        private double salary;
        public string Names
        {
            get { return name; }
            set { name = value; }
        }
        public double Salary
        {
            get { return salary; }
            set { salary = value; }
        }
    }

推荐答案


这篇关于在wpf中动态地将列添加到DataGrid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 15:26