数据网格中的文本对齐——>c# WPF
本文关键字:WPF 对齐 文本 数据网 网格 数据 | 更新日期: 2023-09-27 18:14:08
大家早上好!我有一个简短的问题。
我有一个绑定在项目源上的数据网格,现在我尝试格式化单元格的对齐属性。
<DataGrid x:Name="dgrid"
Margin="10,13,386,0"
SelectionMode="Single"
SelectionUnit="FullRow"
IsReadOnly="True"
ItemsSource="{Binding Patient_view, Mode=TwoWay}"
SelectedItem="{Binding Patient_selected_item, Mode=TwoWay}"
AutoGenerateColumns="False"
CanUserAddRows="False"
TextBlock.TextAlignment="Center"
Height="645"
VerticalAlignment="Top"
HorizontalGridLinesBrush="#FFF5FAF7"
VerticalGridLinesBrush="#FFF5FAF7"
FontFamily="Arial">
<DataGrid.ColumnHeaderStyle>
<Style TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="FontWeight"
Value="Bold" />
<Setter Property="HorizontalAlignment"
Value="Stretch" />
<Setter Property="HorizontalContentAlignment"
Value="Center" />
</Style>
</DataGrid.ColumnHeaderStyle>
<DataGrid.Columns>
<DataGridTextColumn TextBlock.TextAlignment="Left"
Header="Name"
Binding="{Binding Nachname}"
Width="*"
CanUserResize="False" />
<DataGridTextColumn TextBlock.TextAlignment="Left"
Header="Vorname"
Binding="{Binding Vorname}"
Width="*"
CanUserResize="False" />
<DataGridTextColumn TextBlock.TextAlignment="Right"
Header="Versichertennr."
Binding="{Binding Versichertennummer}"
Width="100"
CanUserResize="False" />
<DataGridTextColumn TextBlock.TextAlignment="Left"
Header="Status"
Binding="{Binding Versichertenstatus}"
Width="50"
CanUserResize="False" />
<DataGridTextColumn TextBlock.TextAlignment="Left"
Header="IK"
Binding="{Binding Kostentraegerlk}"
Width="50"
CanUserResize="False" />
<DataGridTextColumn TextBlock.TextAlignment="Left"
Header="Geburtsdatum"
Binding="{Binding Geburtsdatum, StringFormat='{0:dd.MM.yyyy'}}"
Width="95"
CanUserResize="False" />
<DataGridTextColumn TextBlock.TextAlignment="Left"
Header="Strasse"
Binding="{Binding StrasseHausnummer}"
Width="300"
CanUserResize="False" />
<DataGridTextColumn TextBlock.TextAlignment="Right"
Header="PLZ"
Binding="{Binding Postleitzahl}"
Width="60"
CanUserResize="False" />
</DataGrid.Columns>
谁能告诉我为什么TextBlock。对齐不起作用?也许该怎么做呢?我看到了x:Key模板的一些解决方案,但我不能使用x:Key,因为:
The "Key" attribute can only be used on an element taht is contained in "IDictionary".
所以我不能使用它们(
)我有点困惑,对xaml很陌生。
如果有人能帮我,那就太好了。由于你可以应用ElementStyle
上的DataGridTextColumn,有TextBlock和右对齐,它会工作。
<DataGrid x:Name="dgrid" Margin="10,31,10,-233" IsReadOnly="True"
AutoGenerateColumns="False" CanUserAddRows="False" TextBlock.TextAlignment="Center" Height="627" VerticalAlignment="Top" HorizontalGridLinesBrush="#FFF5FAF7" VerticalGridLinesBrush="#FFF5FAF7" FontFamily="Arial" >
<DataGrid.ColumnHeaderStyle>
<Style TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="FontWeight"
Value="Bold" />
<Setter Property="HorizontalAlignment" Value="Stretch" />
<Setter Property="HorizontalContentAlignment" Value="Center" />
</Style>
</DataGrid.ColumnHeaderStyle>
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding Nachname}" Width="*" CanUserResize="False" >
<DataGridTextColumn.ElementStyle>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="HorizontalAlignment" Value="Center" />
</Style>
</DataGridTextColumn.ElementStyle>
</DataGridTextColumn>
<DataGridTextColumn TextBlock.TextAlignment="Left" Header="Vorname" Binding="{Binding Vorname}" Width="*" CanUserResize="False" >
<DataGridTextColumn.ElementStyle>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="HorizontalAlignment" Value="Right" />
</Style>
</DataGridTextColumn.ElementStyle>
</DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>