1

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

虽然无法让它工作..仍然是白色的前景......

4

1 回答 1

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 属性时,它都会将样式添加到默认键下的资源中。因此,每当将上述样式应用于 ComboBox 时,内部的 TextBox 样式都会添加到 ComboBox 的资源中,从而使 TextBox 获得此样式。

从这里很容易实现您正在寻找的内容:只需将 ComboBox 样式放在您的 App.xaml 中以及您的自定义 TextBox 样式中,您就完成了。

于 2010-03-03T17:10:10.113 回答