我目前正在處理自定義畫布,並且我必須添加一個表格,所以我認爲dataGrid會很好。所以我想創建一個「表」從「Datagrid」用戶可以在運行時添加一個表到畫布。動態添加列到WPF中的DataGrid
到現在爲止,我試圖用列表填充DataGrid併成功完成。
如何在運行時將列添加到Datagrid,以便在運行時使用文本框和基於文本框的值從用戶獲取列數和標題值數據網格應添加列和標題值。
其實我想開發一個表,其中用戶傳遞的列數和列標題和表應生成的表。
或者
「您能否提供我的方式,我應該看看,以便對‘’使用DrawingVisual類表」畫
這是GraphicsTable類
的一部分//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> person()
{
List<Person> peep = new List<Person>();
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; }
}
}
如果您可以動態添加列,您是否使用DrawingVisual來繪製網格或者是否願意繼續使用DataGrid? – Dutts 2013-03-27 09:26:29
@Richard感謝您的回覆。 DrawingVisual會很棒!!!但如果它不可能與DrawingVisual一起使用,那麼我必須用datagrid解決。 – Abhinav 2013-03-27 09:32:46