简体   繁体   English

如何在不绑定任何内容的情况下创建 wpf 数据网格?

[英]how to create a wpf datagrid without binding to anything?

I want to create a datagrid without binding to anything.我想创建一个不绑定任何东西的数据网格。 User can add/delete/edit data and the code will collect the data programatically.用户可以添加/删除/编辑数据,代码将以编程方式收集数据。 I did the following but the grid does not allow me to add new row.我做了以下但网格不允许我添加新行。

<DataGrid Name="dgData" CanUserAddRows="True" MinHeight="100">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Column1"></DataGridTextColumn>
                <DataGridTextColumn Header="Column2"></DataGridTextColumn>
            </DataGrid.Columns>
</DataGrid>

When I run the application, the grid is shown with only headers.当我运行应用程序时,网格只显示标题。 Unlike winform DataGridView that gives an empty first row.不像 winform DataGridView 给出一个空的第一行。 There is no way I can add a new row on the GUI.我无法在 GUI 上添加新行。

您仍然需要将ItemsSource设置(或绑定)到支持添加(实现IList )的空集合,还应该将列绑定到item数据类型的属性(或让DataGrid为您创建它们),否则他们赢了“什么都没有。

I know my answer is pretty late, but someone might still come here and benift from it.我知道我的回答已经很晚了,但仍然有人可能会来这里并从中受益。 So basically I did not find a way to fill the DataGrid without Binding in WPF but I found a way to make binding with an anonymous type with dynamically named object properties.所以基本上我没有找到一种方法来填充 DataGrid 而不在 WPF 中绑定,但我找到了一种方法来绑定具有动态命名对象属性的匿名类型。

List<object> myList = new List<object>();
dynamic obj = new ExpandoObject();
((IDictionary<String, Object>)obj)["PropertyName"] = "PropertyValue";
myList.Add(obj);
MyDataGrid.ItemsSource = myList;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM