我在Expression Blend中创建了一种自定义的TextBox
。 我将背景和边框的填充改为渐变,并添加了阴影效果。
我注意到当我将鼠标悬停或聚焦于我的TextBox
时,WPF的某些默认行为或样式会接管并更改我的边框。
我想知道是否有任何方法可以防止或停止WPF在我聚焦或悬停时更改我的TextBox
样式。 这是否可能?
我在Expression Blend中创建了一种自定义的TextBox
。 我将背景和边框的填充改为渐变,并添加了阴影效果。
我注意到当我将鼠标悬停或聚焦于我的TextBox
时,WPF的某些默认行为或样式会接管并更改我的边框。
我想知道是否有任何方法可以防止或停止WPF在我聚焦或悬停时更改我的TextBox
样式。 这是否可能?
更简单的解决方案是将文本框边框厚度设置为0,然后将文本框包裹在您自己的边框中:
<Border BorderBrush="LightGray" BorderThickness="1">
<TextBox Text="{Binding OutlinePlain, Mode=TwoWay, NotifyOnTargetUpdated=True}"
BorderThickness="0"
</TextBox>
</Border>
你应该使用一个新的模板:
<Style TargetType="{x:Type TextBox}">
<Setter Property="SnapsToDevicePixels" Value="True"/>
<Setter Property="OverridesDefaultStyle" Value="True"/>
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
<Setter Property="AllowDrop" Value="true"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TextBoxBase}">
<Border
Name="Border"
CornerRadius="2"
Padding="2"
Background="#FFFFFF"
BorderBrush="#888888"
BorderThickness="1" >
<ScrollViewer Margin="0" x:Name="PART_ContentHost"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter TargetName="Border" Property="Background" Value="#EEEEEE"/>
<Setter TargetName="Border" Property="BorderBrush" Value="#EEEEEE"/>
<Setter Property="Foreground" Value="#888888"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
我移除了触发器IsMouseOver
查看此处以获取更多信息:TextBox Styles and Templates
OverridesDefaultStyle
属性设置为true
?我认为这应该可以防止从默认样式中绘制默认值。Style
/ControlTemplate
中的触发器来覆盖适当属性更改的默认样式机制。<Style x:Key="Triggers" TargetType="TextBox">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property = "BorderBrush" Value="{Binding ToYourBorder}"/>
</Trigger>
</Style.Triggers>
</Style>
<Setter Property="OverridesDefaultStyle" Value="True"/>
?如果是这样,您是否已经将背景定义为StaticResource
? - Grant ThomasOverridesDefaultStyle
被设置为 true
。至于背景,不,它被设置为动态资源 - <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"/>
。更改它没有任何影响。 - mattsven<Microsoft_Windows_Themes:ListBoxChrome>
中的RenderMouseOver="{TemplateBinding IsMouseOver}" RenderFocused="{TemplateBinding IsKeyboardFocusWithin}"
代码片段后,我得到了想要的行为。不过,我的做法是否正确呢? - mattsven
OverridesDefaultStyle
非常重要,必须包含。 - Just code