DataGridTextColumn 的 "TextBox" 样式与其 "TextBlock" 不一致
DataGridTextColumn's "TextBox" styling isn't consistent with its "TextBlock"
我在设置用于在 DataGridTextColumn 中进行编辑的文本框的样式时遇到问题。本专栏的一些背景知识:
- 它绑定到一个名为 'Top' 的 属性(数值)
- 有一个名为 "ShowAll" 的 属性 用作禁用列的触发器
- 如果启用该列,我希望 TextBox 的外观与 TextBlock 的外观相匹配(右对齐,垂直居中)。
单元格在非编辑模式下看起来很完美。
(抱歉缺少图片,但是Whosebug需要10个声望点才能发图片,讽刺的是,这会导致早期发帖效果不佳;愚蠢的规则)
如果我省略 DataGridTextColumn.EditingElementStyle 部分(即,如果我使用默认编辑样式),当单元格获得焦点时,TextBox 在 DataGridTextColumn 中左上对齐:
我希望编辑的值保持右对齐和垂直居中。但是,当我为 EditingElementStyle 添加与为 ElementStyle 所做的相同的两种样式时,背景为蓝色,并且文本框未填充单元格:
我尝试过其他设置器,例如 HorizontalContentAlignment(Stretch 的值),但没有成功。这是我的代码:
<DataGridTextColumn Header="Top" Binding="{Binding Path=Top}" Width="70">
<DataGridTextColumn.ElementStyle>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="HorizontalAlignment" Value="Right" />
<Setter Property="VerticalAlignment" Value="Center" />
</Style>
</DataGridTextColumn.ElementStyle>
<DataGridTextColumn.EditingElementStyle>
<Style TargetType="{x:Type TextBox}">
<Setter Property="HorizontalAlignment" Value="Right" />
<Setter Property="VerticalAlignment" Value="Center" />
</Style>
</DataGridTextColumn.EditingElementStyle>
<DataGridColumn.CellStyle>
<Style TargetType="DataGridCell">
<Style.Triggers>
<DataTrigger Binding="{Binding ShowAll}" Value="False">
<Setter Property="Foreground" Value="Transparent"/>
<Setter Property="Background" Value="LightGray"/>
<Setter Property="IsEnabled" Value="False"/>
</DataTrigger>
</Style.Triggers>
</Style>
</DataGridColumn.CellStyle>
</DataGridTextColumn>
蓝色可能是 SystemColors 中的高亮键刷之一
(见List of SystemColors)
为了填充单元格的宽度,您可以尝试将文本框的宽度绑定到单元格,如下所示:
<Setter Property="Width" Value="{Binding ActualWidth, RelativeSource={RelativeSource Mode=FindAncestor,
AncestorType={x:Type DataGridTextColumn}}}"/>
编辑:
有趣的是,在测试您的 xaml 时,将 HorizontalAlignment 更改为 HorizontalContentAlignment 对我有用:)
<DataGridTextColumn.EditingElementStyle>
<Style TargetType="{x:Type TextBox}">
<Setter Property="HorizontalContentAlignment" Value="Right" />
<Setter Property="VerticalAlignment" Value="Center" />...
编辑2;):
事实上,您可以覆盖 SystemColor 默认值,将其添加到您的 xaml 以更改高亮效果:
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Red" />
并在必要时另外更改控制键
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Green" />
请注意,文本画笔的行为可能不同,您可能无法覆盖它们。
有关该问题的更多详细信息,this link 可能会有用。
有关 Ben 建议的解决方案的更多信息:
<DataGridTextColumn.EditingElementStyle>
<Style TargetType="{x:Type TextBox}">
<Setter Property="VerticalContentAlignment" Value="Center" />
<Setter Property="HorizontalContentAlignment" Value="Right" />
<Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="Height" Value="22" />
</Style>
</DataGridTextColumn.EditingElementStyle>
我在设置用于在 DataGridTextColumn 中进行编辑的文本框的样式时遇到问题。本专栏的一些背景知识:
- 它绑定到一个名为 'Top' 的 属性(数值)
- 有一个名为 "ShowAll" 的 属性 用作禁用列的触发器
- 如果启用该列,我希望 TextBox 的外观与 TextBlock 的外观相匹配(右对齐,垂直居中)。
单元格在非编辑模式下看起来很完美。
(抱歉缺少图片,但是Whosebug需要10个声望点才能发图片,讽刺的是,这会导致早期发帖效果不佳;愚蠢的规则)
如果我省略 DataGridTextColumn.EditingElementStyle 部分(即,如果我使用默认编辑样式),当单元格获得焦点时,TextBox 在 DataGridTextColumn 中左上对齐:
我希望编辑的值保持右对齐和垂直居中。但是,当我为 EditingElementStyle 添加与为 ElementStyle 所做的相同的两种样式时,背景为蓝色,并且文本框未填充单元格:
我尝试过其他设置器,例如 HorizontalContentAlignment(Stretch 的值),但没有成功。这是我的代码:
<DataGridTextColumn Header="Top" Binding="{Binding Path=Top}" Width="70">
<DataGridTextColumn.ElementStyle>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="HorizontalAlignment" Value="Right" />
<Setter Property="VerticalAlignment" Value="Center" />
</Style>
</DataGridTextColumn.ElementStyle>
<DataGridTextColumn.EditingElementStyle>
<Style TargetType="{x:Type TextBox}">
<Setter Property="HorizontalAlignment" Value="Right" />
<Setter Property="VerticalAlignment" Value="Center" />
</Style>
</DataGridTextColumn.EditingElementStyle>
<DataGridColumn.CellStyle>
<Style TargetType="DataGridCell">
<Style.Triggers>
<DataTrigger Binding="{Binding ShowAll}" Value="False">
<Setter Property="Foreground" Value="Transparent"/>
<Setter Property="Background" Value="LightGray"/>
<Setter Property="IsEnabled" Value="False"/>
</DataTrigger>
</Style.Triggers>
</Style>
</DataGridColumn.CellStyle>
</DataGridTextColumn>
蓝色可能是 SystemColors 中的高亮键刷之一 (见List of SystemColors)
为了填充单元格的宽度,您可以尝试将文本框的宽度绑定到单元格,如下所示:
<Setter Property="Width" Value="{Binding ActualWidth, RelativeSource={RelativeSource Mode=FindAncestor,
AncestorType={x:Type DataGridTextColumn}}}"/>
编辑: 有趣的是,在测试您的 xaml 时,将 HorizontalAlignment 更改为 HorizontalContentAlignment 对我有用:)
<DataGridTextColumn.EditingElementStyle>
<Style TargetType="{x:Type TextBox}">
<Setter Property="HorizontalContentAlignment" Value="Right" />
<Setter Property="VerticalAlignment" Value="Center" />...
编辑2;): 事实上,您可以覆盖 SystemColor 默认值,将其添加到您的 xaml 以更改高亮效果:
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Red" />
并在必要时另外更改控制键
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Green" />
请注意,文本画笔的行为可能不同,您可能无法覆盖它们。 有关该问题的更多详细信息,this link 可能会有用。
有关 Ben 建议的解决方案的更多信息:
<DataGridTextColumn.EditingElementStyle>
<Style TargetType="{x:Type TextBox}">
<Setter Property="VerticalContentAlignment" Value="Center" />
<Setter Property="HorizontalContentAlignment" Value="Right" />
<Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="Height" Value="22" />
</Style>
</DataGridTextColumn.EditingElementStyle>