使文本在具有多行的数据网格单元格中居中

时间:2018-09-11 12:42:55

标签: wpf datagrid format

我有一个wpf数据网格,其中某些单元格中有一条多条线的描述未居中,只有一条线的单元格居中。

https://i.stack.imgur.com/5vhtL.png

编辑...

只是认为这是一个标准问题,不需要太多信息。

这是数据网格的XAML。

<DataGrid AutoGenerateColumns="False" Style="{StaticResource DataGridMainStyle}"
                 Name="dataGrid_clients" CanUserReorderColumns="False" CanUserSortColumns="False" IsReadOnly="True"
                 Grid.Row="3" Grid.Column="1"
                 Margin="0,0,0,0" SelectionChanged="dataGrid_clients_SelectionChanged"
                 MaxHeight="300"
                 ScrollViewer.HorizontalScrollBarVisibility="Disabled">
        <DataGrid.Columns>
            <DataGridTextColumn Header="{Binding Path=Events_EventsMain_ClientsGrid_EntranceHeader, Source={StaticResource Resources}}" 
                                   Binding="{Binding Path=Name}" />

            <DataGridTemplateColumn Header="">
                <DataGridTemplateColumn.CellTemplate >
                    <DataTemplate>
                        <TextBlock Text="{Binding IsOpen}" HorizontalAlignment="Center" Padding="0" TextAlignment="Center" />
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>

            <DataGridTextColumn Header="{Binding Path=Events_EventsMain_ClientsGrid_LastHeardFromHeader, Source={StaticResource Resources}}" 
                                   Binding="{Binding Path=LastHeardFrom}" />
            <DataGridTemplateColumn CanUserSort="False" MinWidth="18" CanUserResize="False">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Grid>
                            <Rectangle Stroke="Black" Fill="White" Width="17" Height="17"
                                       HorizontalAlignment="Right">
                            </Rectangle>
                            <Rectangle Name="colorRect" Stroke="Black" Fill="{Binding StatusBrush}" 
                                       Width="13"
                                       Height="13"
                                       HorizontalAlignment="Right"
                                       Margin="0,0,2,0"
                                   ></Rectangle>

                        </Grid>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <DataGridTextColumn Header="{Binding Path=Events_EventsMain_ClientsGrid_StatusHeader, Source={StaticResource Resources}}" Binding="{Binding StatusText}" />

            <DataGridTemplateColumn Width="200*">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <!--<StackPanel Orientation="Horizontal">-->
                        <!--<Button Template="{StaticResource ButtonTextTemplate}" HorizontalAlignment="Right" Style="{StaticResource ButtonText}">Hard restart</Button>-->
                        <Button Template="{StaticResource ButtonTextTemplate}" Content="{Binding Path=Events_EntranceControl_Reboot, Source={StaticResource Resources}}" Visibility="{Binding Path=UserCanRestart , Converter={StaticResource CanRestartVisibilityConverter}}" Click="button_restart_Click" CommandParameter="{Binding Path=HardwareID}" HorizontalAlignment="Right" Style="{StaticResource ButtonText}">
                        </Button>
                        <!--</StackPanel>-->
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <DataGridTemplateColumn Width="Auto" x:Name="HardRestartColumn" Visibility="Collapsed">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Button Template="{StaticResource ButtonTextTemplate}" HorizontalAlignment="Right" CommandParameter="{Binding Path=HardwareID}" Name="HardRestartButton" Click="HardRestartButton_Click"  Style="{StaticResource ButtonText}">Hard restart</Button>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>
    <StackPanel Orientation="Horizontal"  Grid.Row="4" Grid.Column="1" VerticalAlignment="Bottom">
        <Label Content="{Binding Path=Events_EventsMain_LogGrid_Header, Source={StaticResource Resources}}" HorizontalContentAlignment="Left" 
               Style="{StaticResource LabelMediumSize}"></Label>

        <Button Name="buttonLogFilter"
              Height="20" Width="100" Grid.Column="1" Margin="35,2,2,2"  HorizontalAlignment="Left" 
                BorderThickness="0.5"
              Content="{Binding Path=LogGrid_ButtonFilter, Source={StaticResource Resources}}"></Button>

        <CheckBox Name="checkBoxShowPatronNo"
              Margin="5,2,2,2"
                  VerticalAlignment="Center"
                  VerticalContentAlignment="Center"
              Content="{Binding Path=LogGrid_CheckboxShowPatronNo, Source={StaticResource Resources}}" Visibility="Visible" Checked="CheckBoxShowPatronNo_OnChecked" Unchecked="CheckBoxShowPatronNo_OnChecked"></CheckBox>
    </StackPanel>

    <Line Grid.Row="4" Grid.Column="1" Stroke="#4037495D"
          X2="1" StrokeThickness="1"
          Stretch="Fill" VerticalAlignment="Bottom"
          Margin="0,0,0,2" Height="1">
    </Line>
    <!--Style="{StaticResource DataGridMainStyle2}"-->
    <DataGrid AutoGenerateColumns="False"  Style="{StaticResource DataGridMainStyle}"
                 Name="dataGrid_events" 
                 Grid.Row="6" Grid.Column="1"
                 Margin="0,0,0,0"
                 RowHeight="25">
        <DataGrid.RowStyle>
            <Style TargetType="{x:Type DataGridRow}" BasedOn="{StaticResource MTT_RowStyle}">
                <!--<EventSetter Event="PreviewMouseDoubleClick" Handler="Row_PreviewMouseDoubleClick" />-->
                <Setter Property="Margin" Value="0,0,0,0"/>

            </Style>
        </DataGrid.RowStyle>
        <DataGrid.CellStyle>
            <Style  TargetType="{x:Type DataGridCell}" BasedOn="{StaticResource MTT_CellStyle}">
                <!--<Setter Property="Margin" Value="0,6,0,0"/>-->
                <!--<Setter Property="VerticalAlignment" Value="Bottom"></Setter>-->
                <!--<Setter Property="VerticalContentAlignment" Value="Bottom"></Setter>-->
            </Style>
        </DataGrid.CellStyle>
        <DataGrid.Columns>
            <DataGridTextColumn Header="{Binding Path=LogGrid_TimeHeader, Source={StaticResource Resources}}" 
                                   Binding="{Binding Path=ServerTime}" />
            <DataGridTextColumn Header="{Binding Path=LogGrid_ClientTimeHeader, Source={StaticResource Resources}}" 
                                   Binding="{Binding Path=ClientTime}" x:Name="ClientTime" />
            <DataGridTextColumn Header="{Binding Path=LogGrid_EntranceHeader, Source={StaticResource Resources}}" 
                                   Binding="{Binding Path=ClientName}" />
            <DataGridTextColumn Header="{Binding Path=LogGrid_CategoryHeader, Source={StaticResource Resources}}" 
                                   Binding="{Binding Path=Category, Converter={StaticResource FunctionalEventCategoryConverter}}" />
            <DataGridTextColumn Header="{Binding Path=LogGrid_NoteHeader, Source={StaticResource Resources}}" x:Name="NoteColume"
                                   Binding="{Binding Path=Note}" />
            <DataGridTextColumn Header="{Binding Path=LogGrid_GateHeader, Source={StaticResource Resources}}" x:Name="GateColume"
                                   Binding="{Binding Path=Gate}" />
            <DataGridTextColumn Header="{Binding Path=LogGrid_InfoHeader, Source={StaticResource Resources}}" x:Name="UserInfoColumn"
                                   Binding="{Binding Path=UserInfo}" />
            <DataGridTemplateColumn Header="Description"
                                       x:Name="dataColumn">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Path=Description}" TextWrapping="NoWrap"  MaxWidth="500"
                            ToolTip="{Binding Path=Description}">
                        </TextBlock>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <DataGridTextColumn Header="{Binding Path=LogGrid_DoorHeader, Source={StaticResource Resources}}" 
                                   x:Name="doorCountColumn"
                                   Binding="{Binding Path=DoorName}" />
            <DataGridTextColumn Header="{Binding Path=LogGrid_PatronNoHeader, Source={StaticResource Resources}}" 
                                   x:Name="patronColumn"
                                   Binding="{Binding Path=PatronNo, Converter={StaticResource PatronNoConverter}}" />
            <DataGridTextColumn Header="{Binding Path=LogGrid_PatronNameHeader, Source={StaticResource Resources}}" 
                                   x:Name="patronName"
                                   Binding="{Binding Path=PatronName}" />
            <DataGridTextColumn Header="{Binding Path=LogGrid_PatronAddressHeader, Source={StaticResource Resources}}" 
                                   x:Name="patronAddress"
                                   Binding="{Binding Path=PatronAddress}" />
            <DataGridTextColumn Header="{Binding Path=LogGrid_PatronPhoneHeader, Source={StaticResource Resources}}" 
                                   x:Name="patronPhone"
                                   Binding="{Binding Path=PatronPhone}" />
            <DataGridTextColumn Header="{Binding Path=LogGrid_PatronEmailHeader, Source={StaticResource Resources}}" 
                                   x:Name="patronEmail"
                                   Binding="{Binding Path=PatronEmail}" />

        </DataGrid.Columns>
    </DataGrid>

这是我正在使用的样式

    <Color x:Key="BibliothecaRedColor">#ED2225</Color>
<Color x:Key="BibliothecaRedOriginalColor">#FF0000</Color>
<Color x:Key="BibliothecaDarkGrayColor">#343434</Color>
<Color x:Key="BibliothecaLightGrayColor">#E6E7E8</Color>
<Color x:Key="BibliothecaMediumGrayColor">#B3B3B3</Color>
<SolidColorBrush x:Key="BibliothecaRed" Color="{StaticResource ResourceKey=BibliothecaRedColor}"></SolidColorBrush>
<SolidColorBrush x:Key="BibliothecaRedOriginal" Color="{StaticResource ResourceKey=BibliothecaRedOriginalColor}"></SolidColorBrush>
<SolidColorBrush x:Key="BibliothecaDarkGrey" Color="{StaticResource ResourceKey=BibliothecaDarkGrayColor}"></SolidColorBrush>
<SolidColorBrush x:Key="BibliothecaLightGrey" Color="{StaticResource ResourceKey=BibliothecaLightGrayColor}"></SolidColorBrush>
<SolidColorBrush x:Key="BibliothecaMediumGray" Color="{StaticResource ResourceKey=BibliothecaMediumGrayColor}"></SolidColorBrush>

    <Style x:Key="MTT_RowStyle" TargetType="{x:Type DataGridRow}">
    <Setter Property="Height" Value="25"/>
    <!--<Setter Property="Background" Value="Transparent" />-->
    <Setter Property="BorderBrush" Value="Transparent" />

    <Style.Triggers>
        <Trigger Property="IsSelected" Value="True">
            <Setter Property="Background" Value="{StaticResource BibliothecaMediumGray}" />
            <Setter Property="Foreground" Value="White"/>
            <Setter Property="BorderBrush" Value="Transparent" />
        </Trigger>
    </Style.Triggers>
</Style>

<Style x:Key="MTT_CellStyle" TargetType="{x:Type DataGridCell}">
    <!--<Setter Property="Background" Value="Transparent"/>-->
    <Setter Property="BorderBrush" Value="Transparent"/>
    <Setter Property="Padding" Value="5" />
    <Setter Property="VerticalContentAlignment" Value="Center"></Setter>
    <Setter Property="VerticalAlignment" Value="Center"></Setter>
    <Style.Triggers>
        <Trigger Property="IsSelected" Value="True">
            <Setter Property="Background" Value="{x:Null}" />
            <Setter Property="BorderBrush" Value="{x:Null}" />
        </Trigger>
    </Style.Triggers>
</Style>

<Style x:Key="{x:Type DataGridRowHeader}" TargetType="{x:Type DataGridRowHeader}">
    <Setter Property="Background" Value="Red" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type DataGridRowHeader}">
                <Grid>
                </Grid>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="Background" Value="Green" />
                    </Trigger>
                    <Trigger Property="IsRowSelected" Value="True">
                        <Setter Property="Background" Value="Blue" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

<!--Old datagrid header column color #FF8496AD-->
<Style  
       TargetType="{x:Type DataGridColumnHeader}">
    <Setter Property="SnapsToDevicePixels" Value="True" />
    <Setter Property="MinWidth" Value="0" />
    <Setter Property="MinHeight" Value="28" />
    <Setter Property="Foreground" Value="{StaticResource BibliothecaLightGrey}" />
    <Setter Property="FontWeight" Value="SemiBold"></Setter>
    <Setter Property="Cursor" Value="Hand" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type DataGridColumnHeader}">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="Auto" />
                    </Grid.ColumnDefinitions>
                    <Border x:Name="BackgroundBorder" BorderThickness="0,1,0,1" 
                            Background="{StaticResource BibliothecaDarkGrey}" 
                            BorderBrush="{StaticResource BibliothecaDarkGrey}" 
                            Grid.ColumnSpan="2" />
                    <ContentPresenter Margin="6,3,6,3" VerticalAlignment="Center" />
                    <Path x:Name="SortArrow" Visibility="Collapsed" Data="M0,0 L1,0 0.5,1 z" Stretch="Fill" 
                          Grid.Column="1" Width="8" Height="6" Fill="White" Margin="0,0,8,0" 
                          VerticalAlignment="Center" RenderTransformOrigin="0.5,0.4" />
                </Grid>
                <ControlTemplate.Triggers>
                    <Trigger Property="SortDirection" Value="{x:Null}">
                        <Setter TargetName="BackgroundBorder" Property="Background" 
                                Value="#343434" />
                        <Setter TargetName="BackgroundBorder" Property="BorderBrush"  
                                Value="Transparent" />
                    </Trigger>
                    <MultiTrigger>
                        <MultiTrigger.Conditions>
                            <Condition Property="IsMouseOver" Value="True" />
                            <Condition Property="SortDirection" Value="{x:Null}" />
                        </MultiTrigger.Conditions>
                        <Setter Property="Background" TargetName="BackgroundBorder" 
                                Value="#505050" />
                        <Setter Property="BorderBrush" TargetName="BackgroundBorder" 
                                Value="Transparent" />
                    </MultiTrigger>
                    <Trigger Property="SortDirection" Value="Ascending">
                        <Setter TargetName="SortArrow" Property="Visibility" Value="Visible" />
                        <Setter TargetName="SortArrow" Property="RenderTransform">
                            <Setter.Value>
                                <RotateTransform Angle="180" />
                            </Setter.Value>
                        </Setter>
                    </Trigger>
                    <Trigger Property="SortDirection" Value="Descending">
                        <Setter TargetName="SortArrow" Property="Visibility" Value="Visible" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

<Style x:Key="DataGridMainStyle"  TargetType="{x:Type DataGrid}">
    <Setter Property="AlternatingRowBackground" Value="{StaticResource BibliothecaLightGrey}" />
    <Setter Property="BorderBrush" Value="Transparent"/>
    <Setter Property="Background" Value="Transparent" />
    <Setter Property="HeadersVisibility" Value="All" />
    <Setter Property="AlternationCount" Value="2" />
    <Setter Property="RowStyle" Value="{StaticResource MTT_RowStyle}" />
    <Setter Property="HorizontalGridLinesBrush" Value="Transparent" />
    <Setter Property="CellStyle" Value="{StaticResource MTT_CellStyle}" />
    <Setter Property="VerticalGridLinesBrush" Value="Transparent" />
    <Setter Property="IsReadOnly" Value="True"></Setter>
    <Setter Property="CanUserAddRows" Value="False"></Setter>
</Style>

谢谢 安德斯

1 个答案:

答案 0 :(得分:0)

我相信,这是因为这些单元格的数据上下文本身已格式化。由于它是字符串,因此在将数据传递到DataGrid之前,请尝试在后面的代码中进行一些修整(并可能删除新行)。

另外,您有:

<DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Path=Description}" TextWrapping="NoWrap"  MaxWidth="500"
                        ToolTip="{Binding Path=Description}">
                    </TextBlock>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>

您尝试设置TextWrapping="Wrap"吗?最后,澄清一下,如果您想进行水平居中或垂直居中,则可以添加一些信息。