<Style x:Key="TextInputStyle" TargetType="TextBox" BasedOn="{StaticResource {x:Type TextBox}}">
<Setter Property="FontSize" Value="12"/>
<Setter Property="Margin" Value="5,5,5,5"/>
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="Background" Value="Red"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Border x:Name="bg" BorderBrush="#FF7F98DC" BorderThickness="1">
<ScrollViewer x:Name="PART_ContentHost" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="BorderBrush" TargetName="bg" Value="#FF7E97F0"/>
<Setter Property="BorderThickness" TargetName="bg" Value="2"/>
</Trigger>
<Trigger Property="IsFocused" Value="True">
<Setter Property="BorderBrush" TargetName="bg" Value="DarkBlue"/>
<Setter Property="BorderThickness" TargetName="bg" Value="2"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
这是我的App.xaml代码的一部分。
是textBox的样式。
但背景属性不起作用。
其他人工作得很好。
请帮帮我。为什么我不能改变TextBox的背景颜色?
答案 0 :(得分:1)
您正在为文本框添加背景,而应将背景赋予边框,因为它已在控件模板中定义。
<Style x:Key="TextInputStyle" TargetType="TextBox" BasedOn="{StaticResource {x:Type TextBox}}">
<Setter Property="FontSize" Value="12"/>
<Setter Property="Margin" Value="5,5,5,5"/>
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="Foreground" Value="AliceBlue"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Border x:Name="bg" BorderBrush="#FF7F98DC" BorderThickness="1" ***Background="Red"***>
<ScrollViewer x:Name="PART_ContentHost" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="BorderBrush" TargetName="bg" Value="#FF7E97F0"/>
<Setter Property="BorderThickness" TargetName="bg" Value="2"/>
</Trigger>
<Trigger Property="IsFocused" Value="True">
<Setter Property="BorderBrush" TargetName="bg" Value="DarkBlue"/>
<Setter Property="BorderThickness" TargetName="bg" Value="2"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
请参阅上面ControlTemplate内的Border标签。