[英]How do I set alignment on DataGridTextColumn cell text using code
I'm trying to add DataGridTextColumn
programmatically, and set some properties to present the text in cells. 我正在尝试以编程方式添加DataGridTextColumn
,并设置一些属性以在单元格中显示文本。 I don't want to use TemplateColumn
as it requires double-tab to enter edit mode. 我不想使用TemplateColumn
因为它需要双标签才能进入编辑模式。 I can't find code that would set alignment of the text. 我找不到可以设置文本对齐方式的代码。 Anyone has any idea how to achieve this? 任何人都知道如何实现这一目标吗? Thanks in advance. 提前致谢。
I tried so far: 到目前为止,我尝试过:
Dim txt As New DataGridTextColumn()
Dim c As New DataGridCell
c.HorizontalAlignment = HorizontalAlignment.Center
txt.CellStyle = c.Style
and 和
txt.SetValue(TextBox.HorizontalAlignmentProperty, HorizontalAlignment.Center)
and few others that I didn't keep track of. 还有其他一些我没有追踪的东西。
Create a style, and then assign it to txt.CellStyle. 创建一个样式,然后将其分配给txt.CellStyle。
C# code : C#代码:
Style s = new Style();
s.Setters.Add(new Setter(DataGridCell.HorizontalAlignmentProperty, HorizontalAlignment.Center));
txt.CellStyle = s;
Thanks for your help and I found the solution only with your guidance. 感谢您的帮助,我只有在您的指导下才能找到解决方案。 Especially thanks to @AnjumSKhan 特别感谢@AnjumSKhan
This worked for me: 这对我有用:
Dim txt As New DataGridTextColumn()
Dim s As New Style
s.Setters.Add(New Setter(TextBox.TextAlignmentProperty, TextAlignment.Right))
txt.CellStyle = s
In your c# code you have to use the index of the column. 在您的C#代码中,您必须使用列的索引。 Here are some examples to set the cell alignment: 以下是一些设置单元格对齐的示例:
dataGrid.Columns[0].ItemStyle.HorizontalAlign = HorizontalAlign.Center;
dataGrid.Columns[0].ItemStyle.VerticalAlign = VerticalAlign.Middle;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.