自动生成 bool 类型的 Datagrid 列
我正在使用 WPF 数据网格。我绑定到网格的数据没有固定数量的列,因此我将 AutoGenerateColumns 设置为 True。
<DG:TestDataGrid x:Name="grTestData" ItemsSource="{Binding}" LoadingRow="grTestData_LoadingRow" UnloadingRow="grTestData_UnloadingRow" AutoGeneratingColumn="grTestData_AutoGeneratingColumn" AutoGenerateColumns="True" Visibility="Visible" SelectionUnit="CellOrRowHeader"></DG:TestDataGrid>
当使用自动生成的列时,只要遇到 bool 类型的列,它就会使用 DataGridCheckbox 列。当用户关闭此屏幕时,我将分配给数据网格的数据保存在 xml 文件中。
ds.Tables[0].WriteXml(path);
//here path is the place where I am storing the xml
再次,当用户打开屏幕时,我从 xml 加载数据并将其分配回网格。
ds.ReadXmlSchema(path);
ds.ReadXml(path);
grTestData.ItemsSource=ds.Table[0].DefaultView
现在的问题是,布尔列不显示复选框。对于之前选中的复选框,它在“True”列中显示文本值;对于未选中的复选框,在“True”列中显示文本值。请指导我,我该如何预防。如果您需要有关代码的任何其他信息...请提及。我将其粘贴在这里。
问候, 普里扬克
I am using WPF datagrid. Data I am binding to the grid is not having fixed number of columns, so I have set AutoGeneratedColumns to True.
<DG:TestDataGrid x:Name="grTestData" ItemsSource="{Binding}" LoadingRow="grTestData_LoadingRow" UnloadingRow="grTestData_UnloadingRow" AutoGeneratingColumn="grTestData_AutoGeneratingColumn" AutoGenerateColumns="True" Visibility="Visible" SelectionUnit="CellOrRowHeader"></DG:TestDataGrid>
As auto-generated columns are used, where ever it encounters column of type bool it uses DataGridCheckbox columns. When user is closing the this screen, I am saving data assigned to data grid in an xml file.
ds.Tables[0].WriteXml(path);
//here path is the place where I am storing the xml
again when user is opening the sceen I am loading data from xml and assigning it back to grid.
ds.ReadXmlSchema(path);
ds.ReadXml(path);
grTestData.ItemsSource=ds.Table[0].DefaultView
Now the problem is, boolean columns are not showing checkboxes. It is showing text values in the columns "True" for checkboxes which were checked earlier and false for unchecked checkboxes. Please guide me, how can I prevent it. If you need any other info on code... plz mention it. I will paste it here.
Regards,
Priyank
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
通过将 System.Data.XmlWriteMode.WriteSchema 传递到 WriteXML 来指定写入模式。
Specify the writng mode by passing System.Data.XmlWriteMode.WriteSchema into WriteXML.
DataGrid 无法知道属性的“预期”类型是什么,当您读取 XML 时,您需要将其解析为强类型数据结构,现在一切都已考虑是一个
字符串
。There is no way for the
DataGrid
to know what the "intended" type of the attribute is, when you read the XML you need to parse it to a strongly typed data-structure, right now everything is considered to be astring
.