WPF,如何覆盖上特定位置的TextBlocks应用广泛的风格(WPF, how to overri

2019-10-20 03:50发布

我这是在Application.xaml添加一个风格XAML资源字典。 在这种样式文件我指定所有的TextBlocks应该有前景的白色。 问题是,这将改变在用户控件我有同样的应用前景白色组合框项目。 我想的项目在所有黑色前景还是只有这一个组合框。 我有带来很大麻烦使这种情况发生。

这是我的TextBlocks全局样式:

 <Style TargetType="{x:Type TextBlock}" >
        <Setter Property="Foreground">
            <Setter.Value>
                White
            </Setter.Value>
        </Setter>
        <Setter Property="Height">
            <Setter.Value>
                23
            </Setter.Value>
        </Setter>
  </Style>

另外:用户控件的代码隐藏动态地添加组合框。

可以这样做? 怎么样?

我根据雷伯恩斯评论所做的更改。 这是我的MyCustomStyler:

Public Class MyCustomStyler
    Inherits DependencyObject

    Public Shared Function GetStyle1(ByVal obj As DependencyObject) As Style
        Return obj.GetValue(Style1Property)
    End Function
    Public Shared Sub SetStyle1(ByVal obj As DependencyObject, ByVal value As Style)
        obj.SetValue(Style1Property, value)
    End Sub

    Public Shared instancePropertyChangedCallback As New PropertyChangedCallback(AddressOf PropertyChangedCallback_Handler)

    Public Shared ReadOnly Style1Property As DependencyProperty = _
                           DependencyProperty.RegisterAttached("Style1", _
                           GetType(Style), GetType(MyCustomStyler), _
                           New FrameworkPropertyMetadata(instancePropertyChangedCallback))

    Public Shared Sub PropertyChangedCallback_Handler(ByVal obj As DependencyObject, ByVal e As DependencyPropertyChangedEventArgs)
        Dim element = CType(obj, FrameworkElement)
        Dim style = CType(e.NewValue, Style)
        element.Resources(style.TargetType) = style
    End Sub

End Class

这是我的风格部分:

<Style TargetType="ComboBox">
        <Setter Property="local:MyCustomStyler.Style1">
            <Setter.Value>
                <Style TargetType="{x:Type TextBlock}">
                    <Setter Property="Foreground" Value="Black" />
                </Style>
            </Setter.Value>
        </Setter>
    </Style>

不能让它工作,虽然..还有白色的前景...

Answer 1:

来看看这个问题的方法是:怎样才能让组合框使用不同的文本框款式比我的UI的休息吗?

答案是:创建一个应用其他样式的附加属性,然后使用ComboBox样式以应用附加属性。

首先,组合框样式所以你会看到这样的发展方向:

<Style TargetType="ComboBox">
  <Setter Property="local:MyCustomStyler.Style1">
    <Setter.Value>
      <Style TargetType="TextBox">
        <Setter Property="Background" Value="Black" />
      </Style>
    </Setter.Value>
  </Setter>
</Style>

现在,这个工作你需要定义MyCustomStyler类,这将是这样的:

public class MyCustomStyler
{
  public static Style GetStyle1(DependencyObject obj) { return (Style)obj.GetValue(Style1Property); } 
  public static void SetStyle1(DependencyObject obj, Style value) { obj.SetValue(Style1Property, value); } 
  public static readonly DependencyProperty Style1Property = DependencyProperty.RegisterAttached("Style1", typeof(Style), typeof(MyCustomStyler), new PropertyMetadata 
  { 
    PropertyChangedCallback = (obj, e) => 
    {
      var element = obj as FrameworkElement;
      var style = e.NewValue as Style;
      element.Resources[style.TargetType] = style;
    }
  });
}

其工作原理是,每次你设置附加属性STYLE1在FrameworkElement的(如ComboBox)时,它添加样式下的资源的默认密钥。 所以每当上面显示样式应用于组合框,内部文本框样式将被添加到组合框的资源,导致文本框来获得这种风格。

从这里,它是简单的实现你在找什么:只要把组合框风格在你的App.xaml旁边您的自定义文本框风格,你就大功告成了。



文章来源: WPF, how to override an application wide style on textblocks on specific places