我在设置用于在 DataGridTextColumn 中进行编辑的 TextBox 的样式时遇到问题。本专栏的一些背景知识:
- 它绑定到一个名为“Top”的属性(数值)
- 有一个名为“ShowAll”的属性用作禁用列的触发器
- 如果启用了该列,我希望 TextBox 的外观与 TextBlock 的外观和感觉相匹配(右对齐,垂直居中)。
该单元格在非编辑模式下看起来很完美。
(抱歉没有图片,但是 Stackoverflow 需要 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>