[英]WPF datagrid - how to set checkbox in template column without binding?
[英]how to create a wpf datagrid without binding to anything?
我想创建一个不绑定任何东西的数据网格。 用户可以添加/删除/编辑数据,代码将以编程方式收集数据。 我做了以下但网格不允许我添加新行。
<DataGrid Name="dgData" CanUserAddRows="True" MinHeight="100">
<DataGrid.Columns>
<DataGridTextColumn Header="Column1"></DataGridTextColumn>
<DataGridTextColumn Header="Column2"></DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
当我运行应用程序时,网格只显示标题。 不像 winform DataGridView 给出一个空的第一行。 我无法在 GUI 上添加新行。
您仍然需要将ItemsSource
设置(或绑定)到支持添加(实现IList
)的空集合,还应该将列绑定到item数据类型的属性(或让DataGrid
为您创建它们),否则他们赢了“什么都没有。
我知道我的回答已经很晚了,但仍然有人可能会来这里并从中受益。 所以基本上我没有找到一种方法来填充 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.