文本框背景颜色不适用于样式

时间:2016-11-01 11:50:48

标签: wpf xaml background

我的文本框位于WPF用户控件和样式正在应用为:

<ResourceDictionary Source="pack://application:,,,/MyStyles1;component/Themes/MyTheme.xaml"/>

样式如下:

<Style x:Key="OutputTextBoxStyle" TargetType="TextBox">
<Setter Property="FontSize" Value="15" />
<Setter Property="Background" Value="Green" />
<Setter Property="Foreground" Value="Blue" />
<Setter Property="BorderBrush" Value="Red" />
<Setter Property="BorderThickness" Value="15" />
<Setter Property="Padding" Value="2" />
</Style>

注意,所有属性(例如Foreground,BorderBrush,BorderThickness等)都按要求工作。 但未应用TextBox背景。

请协助。

2 个答案:

答案 0 :(得分:0)

有两种方法可以将此样式设置为文本框:

  1. 明确使用:

    <TextBox Style="{StaticResource OutputTextBoxStyle}" />
    

    这是通常的方式,当项目中只有几个文本框需要这种风格适用于它们时。

  2. 隐式使用隐式样式。要使隐式样式起作用,您的定义不应包含x:Key定义。

    所以你要么添加第二个定义:

    <Style TargetType="TextBox"
           BasedOn="{StaticResource OutputTextBoxStyle}" />
    

    或从现有样式中删除x:Key定义。

    如果您想要从其他地方明确引用该样式,则需要执行前者。

  3. 您还可以通过指定样式来覆盖样式的特定部分:

    <TextBox Style="{StaticResource OutputTextBoxStyle}"
             Background="{Aqua}" />
    

    <TextBox Background="{Aqua}" />
    

答案 1 :(得分:0)

编辑:我现在才注意到您通过评论与Chris讨论了这个问题,他几乎问你我在下面问了什么。

与我合作:

<Window.Resources>
    <Style x:Key="OutputTextBoxStyle" TargetType="TextBox">
        <Setter Property="FontSize" Value="15" />
        <Setter Property="Background" Value="Green" />
        <Setter Property="Foreground" Value="Blue" />
        <Setter Property="BorderBrush" Value="Red" />
        <Setter Property="BorderThickness" Value="15" />
        <Setter Property="Padding" Value="2" />
    </Style>
</Window.Resources>
<Grid>
    <TextBox Text="123123" HorizontalAlignment="Left" VerticalAlignment="Top"  Style="{DynamicResource OutputTextBoxStyle}"/>
</Grid>

enter image description here

你的TextBox实际上是什么样的(XAML)?

我怀疑您已设置Background属性,该属性覆盖了您的风格中的价值。

相关问题