简体   繁体   English

如何使用代码在DataGridTextColumn单元格文本上设置对齐方式

[英]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.

相关问题 如何使用 WPF 中的代码绑定 DataGridTextColumn 的可见性属性? - How do I bind the visibility property of a DataGridTextColumn using code in WPF? 如何在代码中设置DataGridTextColumn的绑定? - How can I set the binding of a DataGridTextColumn in code? 如何设置DataGridTextColumn文本颜色? - How to set DataGridTextColumn text color? 如何设置DataGrid DataGridTextColumn文本修剪? - How to set DataGrid DataGridTextColumn Text Trimming? 如何设置DataGridTextColumn和其中的文本的边距? - How to set margin for DataGridTextColumn and text inside it? 如何将工具提示添加到DataGridTextColumn - How do I Add a Tooltip To a DataGridTextColumn 如何在DataGridTextColumn WPF XAML中将绑定值设置为TargetNullValue属性 - How do i set a Binding Value to a TargetNullValue property in a DataGridTextColumn WPF XAML 更改datagridcombobox单元格值后,如何在WPF datagridtextcolumn单元格中设置值? - How to set value in WPF datagridtextcolumn cell when the datagridcombobox cell value is changed? 如何在DataGridTextColumn上将XAML中的双精度值格式化为德语格式? - How do I format a double value in XAML to German format on a DataGridTextColumn? 如何使用DatagridTextColumn.ElementStyle使DatagridTextColumn变为只读? - How can i use DatagridTextColumn.ElementStyle to Make DatagridTextColumn readonly?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM