I have a style xaml resource dictionary which is added in Application.xaml. In that style file I specify that all textblocks should have the foreground white. The problem is that this will change the combobox items foreground to white in a usercontrol I have in the same application. I want the items to have a black foreground in all or only this one combobox. I'm having big troubles making that happen.
This is my global style for 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>
Also: The usercontrol adds the combobox dynamically in the code-behind.
Can this be done? How?
I've made changes according to Ray Burns comment. This is my 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
This is my style section:
<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>
Can't get it to work though.. Still white foreground...