如何在不重新调整可编辑单元格中的编辑框的情况下使DataGrid中的内容居中?

时间:2012-04-12 17:39:55

标签: c# wpf xaml

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

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

它运行正常,但是当我编辑单元格的内容时,它看起来像这样:

the text box is too small

我可以使用setter调整textBox的大小:

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

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

text at the wrong place

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

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

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

我的网格选择被搞砸了:

enter image description here

1 个答案:

答案 0 :(得分: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是一行,你的应用需要它吗?如果不是,那么指定SelectionUnit =“Cell”并强制激活选择的颜色为白色,也将解决问题