DataGrid中的文本对齐方式

时间:2013-08-11 22:02:58

标签: wpf xaml datagrid

我正在通过WPF编程。在DataGrid控件中,我需要一种方法来制作单元格的中心内容。我也使用这个代码:

<DataGrid x:Name="dg1" HorizontalAlignment="Left" Margin="10,10,0,0"
    VerticalAlignment="Top" Height="360" Width="498"
    FontFamily="2  Badr" FontSize="18" 
    AlternatingRowBackground="LightCoral" FlowDirection="RightToLeft"
    HorizontalContentAlignment="Center" VerticalContentAlignment="Center" 
    Background="{x:Null}"/>

有什么问题?

8 个答案:

答案 0 :(得分:83)

您需要设置DataGridCell样式

    <DataGrid>
        <DataGrid.Columns>
            <DataGridTextColumn>
                <DataGridTextColumn.ElementStyle>
                    <Style TargetType="TextBlock">
                        <Setter Property="HorizontalAlignment" Value="Center" />
                    </Style>
                </DataGridTextColumn.ElementStyle>
            </DataGridTextColumn>
        </DataGrid.Columns>
    </DataGrid>

答案 1 :(得分:9)

对于那些需要从自定义XAML样式中仅格式化VB.NET中的一个动态DataGrid列的人:

在Application.xaml中:

<Application.Resources>
    <ResourceDictionary>
        <Style x:Key="DataGridCellCentered" TargetType="DataGridCell">
            <Setter Property="TextBlock.TextAlignment" Value="Center" />
        </Style>
    </ResourceDictionary>
</Application.Resources>

在VB.NET代码中:

Me.MyDataGrid.Columns(5).CellStyle = TryFindResource("DataGridCellCentered")

问候!

答案 2 :(得分:7)

如其他答案所述:

<Setter Property="HorizontalAlignment" Value="Center" />

HorizontalAlignment

这会影响任何其他样式,例如背景。要仅将文本居中,请改用:

<Setter Property="TextAlignment" Value="Center" />

textalign

答案 3 :(得分:4)

也许只是创建一种风格:

<Window.Resources>
    <Style TargetType="DataGridCell">
        <Setter Property="HorizontalAlignment" Value="Center" />
    </Style>
</Window.Resources>  

编辑。

答案 4 :(得分:2)

影响所有列

 <Window.Resources>
        <Style TargetType="DataGridColumnHeader">
            <Setter Property="HorizontalAlignment" Value="Stretch"/>
            <Setter Property="HorizontalContentAlignment" Value="Center"/>
        </Style>
 </Window.Resources>

答案 5 :(得分:1)

如果您想将日期置于DataGridTemplateColumn

中心
 <DataGridTemplateColumn SortMemberPath="DataDiNascita" Header="Data di nascita" IsReadOnly="False">
                <DataGridTemplateColumn.CellEditingTemplate>                        
                    <DataTemplate>
                        <DatePicker SelectedDate="{Binding Path=DataDiNascita,Mode=TwoWay}" VerticalAlignment="Center" HorizontalAlignment="Left">                                
                        </DatePicker>                           
                    </DataTemplate>
                </DataGridTemplateColumn.CellEditingTemplate>
                <DataGridTemplateColumn.CellTemplate>                        
                    <DataTemplate>
                        <TextBlock Text="{Binding Path=DataDiNascita,Mode=TwoWay,StringFormat=\{0:dd/MM/yyyy\}}"  VerticalAlignment="Center" HorizontalAlignment="Left">                              
                        </TextBlock>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>

答案 6 :(得分:0)

如何在WPF DataGrid中居中显示文字

var download = function (uri, filename, callback) {                    
    var file = fs.createWriteStream(filename);
    http.get(uri, function (res) {
    res.pipe(file); 
        res.on('end', function () {
            file.end();                           
        });
    });
};

答案 7 :(得分:0)

我认为这取决于填充或固定在datagrid水平内容对齐中的面板不起作用 对于中心内容对齐,您可以像这样使用DataGrid.resource标签

<DataGrid.Resources>
                <Style TargetType="DataGridCell">
                    <Setter Property="HorizontalAlignment" Value="Center"/>
                </Style>
                <Style TargetType="DataGridColumnHeader">
                    <Setter Property="HorizontalContentAlignment" Value="Center"/>
                </Style>
</DataGrid.Resources>

或者看到这个

 <DataGrid x:Name="DgvUsers" AutoGenerateColumns="False" IsReadOnly="True" EnableRowVirtualization="False"
                      HorizontalContentAlignment="Stretch" VerticalContentAlignment="Center" HeadersVisibility="Column"
                      IsTextSearchEnabled="True" FlowDirection="RightToLeft" SelectionMode="Single" FontFamily="/Sandogh.App;component/Font/#Lalezar" >
                <DataGrid.CellStyle>
                    <Style TargetType="DataGridCell">
                        <Setter Property="HorizontalAlignment" Value="Center"/>
                    </Style>
                </DataGrid.CellStyle>
                <DataGrid.Columns>
                    <DataGridTextColumn Header="UserID" Visibility="Collapsed" Binding="{Binding Path=UserID}"/>
                    <DataGridTextColumn Header="FirstName" Binding="{Binding Path=Name}">
                        <DataGridTextColumn.ElementStyle>
                            <Style TargetType="TextBlock">
                                <Setter Property="HorizontalAlignment" Value="Center" />
                            </Style>
                        </DataGridTextColumn.ElementStyle>
                    </DataGridTextColumn>
                    <DataGridTextColumn Header="LastName" Binding="{Binding Path=Family}"/>
                    <DataGridTextColumn Header="Gender" Binding="{Binding Path=TGender}"/>
                    <DataGridTextColumn Header="UserName" Binding="{Binding Path=UserName}"/>
                    <DataGridTextColumn Header="Password" Binding="{Binding Path=Password}"/>
                    <DataGridTextColumn Header="Activity" Binding="{Binding Path=TActivity}"/>
                    <DataGridTextColumn Header="JobID" Visibility="Collapsed" Binding="{Binding Path=JobID}"/>
                    <DataGridTextColumn Header="JobName" Binding="{Binding Path=JobName}"/>
                </DataGrid.Columns>
            </DataGrid>