代码之家  ›  专栏  ›  技术社区  ›  Christian80

WPF,如何在特定位置的文本块上重写应用程序范围的样式

  •  1
  • Christian80  · 技术社区  · 16 年前

    我有一个样式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>
    

    另外:用户控件在代码后面动态添加组合框。

    能做到吗?怎么用?

    这是我的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>
    

    1 回复  |  直到 14 年前
        1
  •  1
  •   Ray Burns    16 年前

    看待这个问题的方法是:如何使组合框使用不同于其他UI的文本框样式?

    答案是:创建应用其他样式的附加属性,然后使用组合框样式应用附加属性。

    首先是组合框样式,这样您就可以看到它的走向:

    <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(如组合框)上设置附加的Style1属性时,它都会在默认键下将样式添加到资源中。因此,无论何时将上述样式应用于组合框,内部文本框样式都将添加到组合框的资源中,从而使文本框获得此样式。

    从这里可以很简单地实现您想要的:只需在App.xaml中将ComboBox样式与自定义的TextBox样式放在一起,就完成了。

    推荐文章