Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
wpf网格中的数据值未正确对齐_Wpf_C# 4.0_Wpfdatagrid - Fatal编程技术网

wpf网格中的数据值未正确对齐

wpf网格中的数据值未正确对齐,wpf,c#-4.0,wpfdatagrid,Wpf,C# 4.0,Wpfdatagrid,我正在使用wpf数据网格。我希望数据在网格中正确且居中对齐。目前我正在使用Style setter属性,但没有任何东西适合我 private void DgvInvoice_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e) { DataGridColumn nextColumn = DgvInvoice.ColumnFromDisplayIndex(7); /

我正在使用wpf数据网格。我希望数据在网格中正确且居中对齐。目前我正在使用Style setter属性,但没有任何东西适合我

private void DgvInvoice_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
        {

                    DataGridColumn nextColumn = DgvInvoice.ColumnFromDisplayIndex(7); // Display Index 7 is Amount Colum in the grid 

                    DataGridRow row = (DataGridRow)DgvInvoice.ItemContainerGenerator.ContainerFromItem(new DataGridCellInfo(DgvInvoice.SelectedItem, nextColumn).Item);

                    DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(row);

                    DataGridCell gridCell = presenter.ItemContainerGenerator.ContainerFromIndex(7) as DataGridCell;
                    if (gridCell != null)
                    {

                        if (d != 0)
                        {
                            gridCell.Content = String.Format("{0:#,0}", d);
                        }
                        else { gridCell.Content = string.Empty; }

                        Style s = new Style();
                        gridCell.Height = 29;
                        s.Setters.Add(new Setter(TextBox.TextAlignmentProperty, TextAlignment.Right));
                        s.Setters.Add(new Setter(TextBox.VerticalContentAlignmentProperty, VerticalAlignment.Center));
                        gridCell.Style = s;
                    }
                }
private void DgvInvoice_CellEditEnding(对象发送方,DataGridCellEditEndingEventArgs e)
{
DataGridColumn nextColumn=DgvInvoice.ColumnFromDisplayIndex(7);//显示索引7是网格中的数量列
DataGridRow行=(DataGridRow)DgvInvoice.ItemContainerGenerator.ContainerFromItem(新DataGridCellInfo(DgvInvoice.SelectedItem,nextColumn).Item);
DataGridCellsPresenter=GetVisualChild(行);
DataGridCell gridCell=presenter.ItemContainerGenerator.ContainerFromIndex(7)作为DataGridCell;
if(gridCell!=null)
{
如果(d!=0)
{
gridCell.Content=String.Format(“{0:#,0}”,d);
}
else{gridCell.Content=string.Empty;}
样式s=新样式();
网格单元高度=29;
s、 添加(新的Setter(TextBox.TextAlignmentProperty,TextAlignment.Right));
s、 添加(新的Setter(TextBox.VerticalContentAlignmentProperty,VerticalAlignment.Center));
Style=s;
}
}
试试这个

Style alignStyle = new Style(typeof(DataGridCell));
alignStyle.Setters.Add(new  Setter(DataGridCell.WidthProperty, 20.0));
alignStyle.Setters.Add(new Setter(DataGridCell.HorizontalAlignmentProperty,HorizontalAlignment.Center));
alignStyle.Setters.Add(new  Setter(DataGridCell.VerticalAlignmentProperty,VerticalAlignment.Center));
YouDataGridName.Style = alignStyle; 

我只为你准备了xaml风格

    <Style x:Key="GridCellStyle" TargetType="{x:Type TextBlock}">
        <Setter Property="HorizontalAlignment" Value="Right" />
        <Setter Property="VerticalAlignment" Value="Right" />
    </Style>

 <DataGridTextColumn Header="Betrag" MinWidth="125"
                Binding="{Binding Path=Betrag, Mode=OneWay, StringFormat=#\,##0.00}"
                ElementStyle="{StaticResource GridCellStyle}" />

注意:此处的标题要求使用“网格”,它与“数据网格”不同