C# 如何在不调整可编辑单元格中编辑框大小的情况下将内容集中在DataGrid中?

C# 如何在不调整可编辑单元格中编辑框大小的情况下将内容集中在DataGrid中?,c#,wpf,xaml,C#,Wpf,Xaml,我使用以下代码使datagrid的单元格在中心显示文本: <Style x:Key="CenteredTextColumn" TargetType="{x:Type DataGridCell}"> <Setter Property="Background" Value="Transparent"/> <Setter Property="BorderBrush" Value="Transparent"/>

我使用以下代码使datagrid的单元格在中心显示文本:

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

我的网格选择被仔细检查:


将其放入拉伸工作的网格中吗?尽管这会为每个单元格添加一个附加控件

<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”并强制活动选择的颜色为白色也将解决问题

是否要包装文本,如果是,请在更新中使用TextWrapping=“wrap”,将网格居中并拉伸内容,在我的帖子中,我建议相反的做法-拉伸网格,并将内容居中:),试试看这是否能解决问题。我改变了这一点,因为它对编辑没有任何影响,文本以相同的方式显示。你是否确保网格不仅拉伸,而且背景为白色?哦。现在我明白了。我已经添加了,现在它工作得非常好!(我的第一次回答应该更具体一点)