Bab*_*bad 38 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}"/>
Run Code Online (Sandbox Code Playgroud)
怎么了?
我能做什么?
请在XAML(Just XAML)中提供您的答案.
Lai*_*290 85
您需要设置DataGridCell样式
<DataGrid>
<DataGrid.Columns>
<DataGridTextColumn>
<DataGridTextColumn.ElementStyle>
<Style TargetType="TextBlock">
<Setter Property="HorizontalAlignment" Value="Center" />
</Style>
</DataGridTextColumn.ElementStyle>
</DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
Run Code Online (Sandbox Code Playgroud)
小智 11
对于那些需要从自定义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>
Run Code Online (Sandbox Code Playgroud)
在VB.NET代码中:
Me.MyDataGrid.Columns(5).CellStyle = TryFindResource("DataGridCellCentered")
Run Code Online (Sandbox Code Playgroud)
问候!
如其他答案所述:
<Setter Property="HorizontalAlignment" Value="Center" />
Run Code Online (Sandbox Code Playgroud)
这将影响任何其他样式,如背景.要仅将文本居中,请使用以下内容:
<Setter Property="TextAlignment" Value="Center" />
Run Code Online (Sandbox Code Playgroud)
也许只是创建一个样式:
<Window.Resources>
<Style TargetType="DataGridCell">
<Setter Property="HorizontalAlignment" Value="Center" />
</Style>
</Window.Resources>
Run Code Online (Sandbox Code Playgroud)
已编辑。