0

我正在使用以下代码让我的数据网格的单元格在中心显示文本:

    <Style x:Key="CenteredTextColumn" TargetType="{x:Type DataGridCell}">
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="BorderBrush" Value="Transparent"/>
        <Setter Property="BorderThickness" Value="1"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type DataGridCell}">
                    <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
                        <ContentPresenter VerticalAlignment="Center" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Setter Property="VerticalAlignment" Value="Stretch"/>
        <Setter Property="HorizontalAlignment" Value="Stretch"/>
        <Setter Property="Foreground" Value="Black">                
        </Setter>
        <Style.Triggers>
            <Trigger Property="IsSelected" Value="True">
                <Setter Property="Background">
                    <Setter.Value>
                        <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                            <GradientStop Color="#FFE3ECF7" Offset="0"/>
                            <GradientStop Color="#FF71DDF9" Offset="1"/>
                            <GradientStop Color="#FF5091DC" Offset="0.546"/>
                        </LinearGradientBrush>
                    </Setter.Value>
            </Setter>
                <Setter Property="Foreground" Value="White"/>
                <Setter Property="BorderBrush" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
            </Trigger>
            <Trigger Property="IsKeyboardFocusWithin" Value="True">
                <Setter Property="BorderBrush" Value="{DynamicResource {x:Static DataGrid.FocusBorderBrushKey}}"/>
            </Trigger>                  
        </Style.Triggers>
    </Style>

它工作正常,除了当我编辑单元格的内容时,它看起来像这样:

文本框太小

我可以使用 setter 调整文本框的大小:

          <Trigger Property="IsEditing" Value="True">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type DataGridCell}">
                            <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
                                <ContentPresenter VerticalAlignment="Stretch" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                            </Border>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>               
            </Trigger>

但这会导致文本框中的文本向上移动:

发错地方的文字

如何使文本框在不移动文本的情况下占用整个单元格?

更新当我使用以下模板时:

<ControlTemplate TargetType="{x:Type DataGridCell}">
                    <Grid VerticalAlignment="Center">
                        <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
                            <ContentPresenter VerticalAlignment="Stretch" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                        </Border>
                    </Grid>
                </ControlTemplate>

我的网格选择得到了整理:

在此处输入图像描述

4

1 回答 1

1

将它放在拉伸工作的网格中吗?虽然这会为每个单元格添加一个额外的控件..

<ControlTemplate TargetType="{x:Type DataGridCell}">
        <Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="White">
            <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
                <ContentPresenter VerticalAlignment="Center" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
            </Border>
        </Grid>
    </ControlTemplate>

另外,感谢屏幕截图,DataGrid 的 SelectionUnit 看起来是一个 Row,您的应用程序需要吗?如果不是,则指定 SelectionUnit="Cell" 并强制活动选择的颜色为白色,也将解决问题

于 2012-04-12T18:03:19.523 回答