如何根据单元格中的值更改datagrid中单元格的格式? [英] How do I change formatting of a cell in datagrid based on the value in the cell?
本文介绍了如何根据单元格中的值更改datagrid中单元格的格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下数据网格
< DataGrid x:名称 = dataGrid ScrollViewer .CanContentScroll = 真 SelectionUnit = Cell < span class =code-attribute>
< span class =code-attribute> 高度 = 自动 保证金 = 5,58, 5,50 ScrollViewer.VerticalScrollBarVisibility = 可见
< span class =code-attribute> AutoGenerateColumns = True AlternatingRowremoved = #FF9BEEE2 IsReadOnly = True AutoGeneratingColumn = dataGrid_AutoGeneratingColumn SelectionMode = 单个 >
< / DataGrid >
有些列包含浮动数字,我想用固定的小数位数格式化数字。
秒问题:
我还想根据值更改单元格的背景颜色。
解决方案
< blockquote>嗨Tommy,
据我所知,你不能通过xaml自动生成列。
你有几个选项:
- 在绑定后迭代列并设置CellStyle或CellTemplate。
- 事先创建列,然后覆盖CellStyle或CellTemplate。
- 用字符串格式的新绑定替换每列的数据绑定。
如果我想出另一个选项,我会再次发表评论。
祝你好运,
Inbar
I have following datagrid
<DataGrid x:Name="dataGrid" ScrollViewer.CanContentScroll="True" SelectionUnit="Cell"
Height="Auto" Margin="5,58,5,50" ScrollViewer.VerticalScrollBarVisibility="Visible"
AutoGenerateColumns="True" AlternatingRowremoved="#FF9BEEE2" IsReadOnly="True" AutoGeneratingColumn="dataGrid_AutoGeneratingColumn" SelectionMode="Single">
</DataGrid>
Some of the columns contain floating number and I would like to format the number with fixed number of decimals.
Second question:
I would also like to change the background color of a cell based on the value.
解决方案
Hi Tommy,
As far as I know, you can't do this with autogenerated columns via xaml.
You have several options to do it:
- Iterate through the columns after the binding and set either the CellStyle or the CellTemplate.
- Create the columns in advance and then override the CellStyle or CellTemplate.
- Replace the data binding of each column with a new binding with a string format.
If I'll come up with another option I'll comment again.
Good luck,
Inbar
这篇关于如何根据单元格中的值更改datagrid中单元格的格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文