本文主要是介绍WPF Win7和Win10 TextBox 使能false时,背景色属性触发不生效问题,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
最近做一个WPF项目,添加一个自定义的整数加减控件,该控件分为三部分,左边是“数值减”,中间是TextBox输入框显示数值、右边是“数值加”;效果图入下图所示。
问题:在Win7系统下,设置该控件使能为False时,TextBox背景色变成白色,样式里面写的触发器不生效;
在Win10系统下,设置该控件IsEnabled="False"时,颜色变化更改正常显示;
故编写该文章,以便以后遇到此类问题,能有点作用。
部分界面xaml代码如下:
<Style TargetType="{x:Type local:NumBntText}"><Setter Property="FontSize" Value="14"/><Setter Property="Template"><Setter.Value><ControlTemplate TargetType="{x:Type local:NumBntText}"><Border CornerRadius="5" BorderThickness="0,0,0,0" BorderBrush="{DynamicResource border_color}" Background="{DynamicResource Backgr_color}" Focusable="False"><Grid Width="{TemplateBinding Width}"><Grid.ColumnDefinitions><ColumnDefinition Width="auto"/><ColumnDefinition/><ColumnDefinition Width="auto"/></Grid.ColumnDefinitions><Button x:Name="nbtnminus" Grid.Column="0" Width="35" Content="-" Style="{StaticResource Button_Text}" BorderBrush="LightBlue"/><TextBox Grid.Column="1" VerticalAlignment="Center" VerticalContentAlignment="Center" BorderThickness="0" Name="textBox" TextAlignment="Center" Background="Transparent" Foreground="{DynamicResource textblock_txt}"Text="{Binding Number,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged, RelativeSource={RelativeSource TemplatedParent}}""/><Button x:Name="nbtn" Grid.Column="2" Width="35" Content="+" Style="{StaticResource Button_TextTwo}" BorderBrush="LightBlue"/></Grid></Border><ControlTemplate.Triggers><Trigger Property="IsEnabled" Value="False"><Setter Property="Background" Value="{DynamicResource Backgr_color}"/><Setter Property="Foreground" Value="{DynamicResource textblock_txt}"/><Setter TargetName="textBox" Property="Background" Value="{DynamicResource Backgr_color}"/></Trigger></ControlTemplate.Triggers></ControlTemplate></Setter.Value></Setter>
</Style>
解决:删除触发器代码 并且 改写TextBox控件模板即可解决该问题;
改写 TextBox 模板代码示例:
<Style TargetType="{x:Type TextBox}"><Setter Property="FontSize" Value="14"/><Setter Property="Background" Value="{DynamicResource Backgr_color}"/><Setter Property="Foreground" Value="{DynamicResource textblock_txt}"/><Setter Property="Template"><Setter.Value><ControlTemplate TargetType="TextBox"><Border Name="Bd" BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}" SnapsToDevicePixels="true"><ScrollViewer Name="PART_ContentHost" Background="{TemplateBinding Background}" Foreground="{TemplateBinding Foreground}" FontSize="{TemplateBinding FontSize}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" FontFamily="{TemplateBinding FontFamily}" /></Border><ControlTemplate.Triggers><Trigger Property="IsEnabled" Value="False"><Setter Property="Background" Value="{DynamicResource Backgr_color}"/><Setter Property="Foreground" Value="{DynamicResource textblock_txt}"/><Setter TargetName="PART_ContentHost" Property="Background" Value="{DynamicResource Backgr_color}"/></Trigger></ControlTemplate.Triggers></ControlTemplate></Setter.Value></Setter>
</Style>
这篇关于WPF Win7和Win10 TextBox 使能false时,背景色属性触发不生效问题的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!