我这是在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>
不能让它工作,虽然..还有白色的前景...