Растянуть ContentPresenter в DataGrid, сохраняя при этом содержимое в середине? - PullRequest
0 голосов
/ 01 мая 2019

Я обнаружил, что самый простой способ получить мои Content по центру и по вертикали бы это:

<Setter Property="Template">
    <Setter.Value>
        <ControlTemplate TargetType="{x:Type DataGridCell}">
            <Grid Background="Gray">
                <ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center" />
            </Grid>
        </ControlTemplate>
    </Setter.Value>
</Setter>

Но мне не очень нравится, что мои действительные TextBox не заполняют всю ячейку при попытке изменить значение ячейки, но вместо этого вокруг значения есть белая рамка, которая выглядит следующим образом:

enter image description here

Нет способа установить HorizontalContentAlignment для ContentPresenter, и хотя я получаю желаемый эффект, используя TextBox в моем ControlTemplate, например:

<Setter Property="Template">
    <Setter.Value>
        <ControlTemplate TargetType="{x:Type DataGridCell}">
            <Grid VerticalAlignment="Stretch">
                <TextBox Text="{Binding Value, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" HorizontalContentAlignment="Center" BorderThickness="0" Background="Gray">
                </TextBox>
            </Grid>
        </ControlTemplate>
    </Setter.Value>
</Setter>

это требует определенного Binding для работы, что наносит ущерб всей цели повторного использования.

Как лучше всего растянуть белое поле на всю ячейку или полностью отключить белое поле?

1 Ответ

0 голосов
/ 06 июня 2019

Вместо того, чтобы работать с ContentPresenter, я нашел способ привязать текстовое поле к контенту, например:

<Style TargetType="DataGridCell" x:Key="TextBoxTemplateCellStyle" BasedOn="{StaticResource DefaultCellStyle}"> 
        <Setter Property="Width" Value="40" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type DataGridCell}">
                    <Grid>
                        <TextBox Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Content.Text}" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" HorizontalContentAlignment="Center" BorderThickness="0"  Background="{Binding RelativeSource={RelativeSource TemplatedParent},Path=Background}">
                            <TextBox.Style>
                                <Style TargetType="{x:Type TextBox}">
                                </Style>
                            </TextBox.Style>
                        </TextBox>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

, что позволяет мне растягивать текстовое поле полностью, не имея маленького белого цвета.коробка внутри моей серой клетки.

...