【问题标题】:WPF, how to override an application wide style on textblocks on specific placesWPF,如何在特定位置的文本块上覆盖应用程序范围的样式
【发布时间】:2010-03-03 11:40:26
【问题描述】:

我有一个样式 xaml 资源字典,它添加到 Application.xaml 中。 在该样式文件中,我指定所有文本块都应具有白色前景。 问题是这会将组合框项目前景更改​​为我在同一应用程序中的用户控件中的白色。我希望这些项目在全部或仅这一个组合框中具有黑色前景。 我很难做到这一点。

这是我对文本块的全局样式:

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

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

这可以吗?怎么样?

我已根据 Ray Burns 的评论进行了更改。 这是我的 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>

虽然不能让它工作..仍然是白色的前景...

【问题讨论】:

    标签: wpf styles textblock


    【解决方案1】:

    看待这个问题的方式是:如何让 ComboBoxes 使用与我的 UI 其余部分不同的 TextBox 样式?

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

    首先是 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;
        }
      });
    }
    

    其工作方式是,每次在 FrameworkElement(例如 ComboBox)上设置附加的 Style1 属性时,它都会将样式添加到默认键下的资源中。所以每当将上面所示的 Style 应用到 ComboBox 时,内部的 TextBox 样式就会被添加到 ComboBox 的资源中,从而使 TextBox 获得该样式。

    从这里很容易实现您正在寻找的内容:只需将 ComboBox 样式与您的自定义 TextBox 样式放在 App.xaml 中即可。

    【讨论】:

    • 我已将样式代码添加到我的应用程序范围的 styles.xaml 并添加了 MyCustomStyler,但它仍然无法正常工作。我在属性或 propertychangedcallback 内的断点都没有被命中。请参阅我的第一个回复中的代码。
    • 我将样式代码更改为我在评论中发布的代码。从 targettype=TextBox 更改为 TextBlock。现在 PropertyChangedCallback 被击中,样式被添加到元素中,但仍然是白色文本而不是黑色。这很奇怪!
    猜你喜欢
    • 2018-02-24
    • 2012-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多