当我的文本框被禁用时,我在给它应用颜色时遇到了问题,
此外,我有数据网格,我用"#E0E4E5"
颜色给行着色。当我的文本框被禁用时,我想保留它的颜色,就像行的颜色是("#E0E4E5"
)一样。
我做的是下一步:
我将属性设置为行AlternatingRowBackground="#E0E4E5"
,然后获得此颜色作为行的背景色。
之后我做了下一步,我为我的文本框做了样式,因为wpf中的默认样式看起来不好,它有一些阴影等,所以这是我的自定义样式的文本框:
<Style x:Key="TextBoxStyle1" TargetType="{x:Type TextBox}">
<Style.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Background" Value="#E0E4E5" />
<Setter Property="BorderBrush" Value="#E0E4E5" />
<Setter Property="BorderThickness" Value="1.5" />
</Trigger>
</Style.Triggers>
<Setter Property="BorderBrush" Value="#0091EA"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.WindowTextBrushKey}}"/>
<Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"/>
<Setter Property="Padding" Value="1"/>
<Setter Property="KeyboardNavigation.TabNavigation" Value="None"/>
<Setter Property="HorizontalContentAlignment" Value="Left"/>
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
<Setter Property="AllowDrop" Value="true"/>
<Setter Property="ScrollViewer.PanningMode" Value="VerticalFirst"/>
<Setter Property="Stylus.IsFlicksEnabled" Value="False"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TextBox}">
<Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"
Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
<ScrollViewer x:Name="PART_ContentHost"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
正如你所看到的,这里有一段代码(triger),它说ok,当你是残疾人时,让你的背景色像这样,边刷像这样:
<Style.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Background" Value="#E0E4E5" />
<Setter Property="BorderBrush" Value="#E0E4E5" />
<Setter Property="BorderThickness" Value="1.5" />
</Trigger>
</Style.Triggers>
下面是一个例子,它看起来是什么样子的:
可以看出,我对它们应用了相同的颜色"#E0E4E5“,但忽略了它们是不同的,所以伙计们,当我的文本框禁用它成为"#E0E4E5”<- colour时,我如何做到这一点呢?
我还必须注意到,如果我改变边界笔刷的颜色,它是有效的。例如,当它们被禁用时,我将它们都设置为textbox,并将边刷设置为红色,并得到以下结果:
所以边框笔刷改变了,但是背景没有改变。
谢谢你们,干杯
发布于 2017-03-13 04:31:56
在禁用ControlTemplate
时,删除将Background
设置为SystemColors.ControlBrushKey
的ControlTemplate
中的SystemColors.ControlBrushKey
:
<Style x:Key="TextBoxStyle1" TargetType="{x:Type TextBox}">
<Style.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Background" Value="#E0E4E5" />
<Setter Property="BorderBrush" Value="#E0E4E5" />
<Setter Property="BorderThickness" Value="1.5" />
</Trigger>
</Style.Triggers>
<Setter Property="BorderBrush" Value="#0091EA"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.WindowTextBrushKey}}"/>
<Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"/>
<Setter Property="Padding" Value="1"/>
<Setter Property="KeyboardNavigation.TabNavigation" Value="None"/>
<Setter Property="HorizontalContentAlignment" Value="Left"/>
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
<Setter Property="AllowDrop" Value="true"/>
<Setter Property="ScrollViewer.PanningMode" Value="VerticalFirst"/>
<Setter Property="Stylus.IsFlicksEnabled" Value="False"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TextBox}">
<Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"
Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
<ScrollViewer x:Name="PART_ContentHost"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
https://stackoverflow.com/questions/42737095
复制相似问题