问题描述
我在WPF中有一个TextBox,当鼠标悬停在TextBox上时,我试图更改边框颜色。根据我在WPF中其他元素的经验,我需要将ControlTemplate值和TemplateBinding插入我要动态更改的值。但是,当我应用此选项时,该框将变得不可编辑(并且文本消失)。如果删除模板设置器,则该框将再次变为可编辑状态,但是自定义BorderBrush触发器不起作用。
这是样式:
<Style x:Key="TextBoxBase" targettype="TextBox">
<Setter Property="FontSize" Value="30"/>
<Setter Property="Background" Value="{StaticResource BrushLightGrey}"/>
<Setter Property="Foreground" Value="{StaticResource BrushnormalText}"/>
<Setter Property="IsReadOnly" Value="False"/>
<Setter Property="Height" Value="40"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate targettype="{x:Type TextBox}">
<Border BorderBrush="{TemplateBinding BorderBrush}">
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsMouSEOver" Value="True">
<Setter Property="BorderBrush" Value="{StaticResource BrushBlue}"/>
</Trigger>
</Style.Triggers>
<Style.Resources>
<Style targettype="{x:Type Border}">
<Setter Property="CornerRadius" Value="5"/>
</Style>
</Style.Resources>
</Style>
任何建议或帮助,我们将不胜感激。谢谢。
解决方法
您错过了关键部分:
<ScrollViewer Margin="0"
x:Name="PART_ContentHost" />
这是承载文本的地方。
请参见
https://docs.microsoft.com/en-us/dotnet/framework/wpf/controls/textbox-styles-and-templates
文本框部件 下表列出了TextBox控件的命名部分。
文本框部件 零件类型说明 PART_ContentHost FrameworkElement一个可视元素,可以包含FrameworkElement。 TextBox的文本显示在此元素中。