Datagrid中的TextAlignment - > C#WPF

时间:2014-07-22 05:20:44

标签: c# wpf xaml datagrid

早上好, 我有一个简短的问题。

我在itemsource上绑定了一个datagrid,现在我尝试格式化单元格的alginment属性。

   <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.Alignment dosnt有效吗? 也许该怎么办? 我看到了一些带有x:Key模板的解决方案,但我不能使用x:Key,因为:

The "Key" attribute can only be used on an element taht is contained in "IDictionary".

所以我无法使用它们:(

我对xaml感到有点困惑和新鲜。

如果有人可以帮助我,那会很好:) 感谢

1 个答案:

答案 0 :(得分:1)

您可以在具有TextBlock的DataGridTextColumn上应用ElementStyle并右对齐,它可以正常工作。

<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>