我有一个样式 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>
虽然无法让它工作..仍然是白色的前景......