C# 按DataGridRowHeader的中心对齐数字

C# 按DataGridRowHeader的中心对齐数字,c#,.net,C#,.net,我正在尝试编辑(在文章末尾滚动),我希望该数字有一个正常的格式,它有对齐,边距等。。。以下是我所改变的: GetVisualChildCollection<DataGridRow>(dataGrid). ForEach(d => { Style style = new Style(typeof(DataGridRowHeader)); styl

我正在尝试编辑(在文章末尾滚动),我希望该数字有一个正常的格式,它有对齐,边距等。。。以下是我所改变的:

            GetVisualChildCollection<DataGridRow>(dataGrid).
                ForEach(d => {
                    Style style = new Style(typeof(DataGridRowHeader));
                    style.Setters.Add(new Setter(DataGridRowHeader.HorizontalContentAlignmentProperty, HorizontalAlignment.Right));

                    TextBlock textBlock = new TextBlock();
                    textBlock.Margin = new Thickness(5, 0, 5, 0);
                    textBlock.HorizontalAlignment = HorizontalAlignment.Right;
                    textBlock.Text = (d.GetIndex() + 1).ToString();

                    d.Header = textBlock;
                    d.HeaderStyle = style;
                });
GetVisualChildCollection(数据网格)。
ForEach(d=>{
样式样式=新样式(typeof(DataGridRowHeader));
添加(新的Setter(DataGridRowHeader.HorizontalContentAlignmentProperty,HorizontalAlignment.Right));
TextBlock TextBlock=新的TextBlock();
textBlock.Margin=新厚度(5,0,5,0);
textBlock.HorizontalAlignment=HorizontalAlignment.Right;
textBlock.Text=(d.GetIndex()+1.ToString();
d、 标题=文本块;
d、 头型=风格;
});
保证金存在。但我对对齐有问题

我想在行标题右侧的文本框中对齐文本

我试图设置文本框和行标题样式的对齐方式,但没有任何效果

这就是我想要的

所以我做了


使用:textBox1.TextAlign=HorizontalAlignment.Center;根据字体的不同,allignment看起来会有所不同。@jdweng我听从了你的建议,但仍然不起作用。我使用了
textBlock.TextAlignment=TextAlignment.Right