Application.xaml に追加されたスタイル xaml リソース ディクショナリがあります。そのスタイル ファイルでは、すべてのテキスト ブロックの前景色を白にするように指定しています。問題は、これにより、同じアプリケーションにあるユーザーコントロールでコンボボックス項目の前景が白に変わることです。すべてまたはこの 1 つのコンボボックスだけでアイテムの前景を黒くしたい。私はそれを実現するのに大きな問題を抱えています。
これは、テキストブロックの私のグローバル スタイルです。
<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>
しかし、それを機能させることはできません..まだ白い前景...