2つのボタンを含むWPF UserControlを作成しようとしています。このUserControlをWindowで使用し、Window.Resource値を適用して、ユーザーコントロール内の1つのボタンの背景を設定します。現在WPF UserControlプロパティバインディング
私が持っている:
window.xaml
<Window.Resources> <SolidColorBrush Color="Brown" x:Key="theBG"></SolidColorBrush> </Window.Resources> <theControl:TheControl x:Name="TheControl" buttonBG="{Binding Source={StaticResource theBG}}" />
usercontrol.xaml.cs
public SolidColorBrush buttonBG { get { return base.GetValue(buttonBGProperty) as SolidColorBrush; } set { base.SetValue(buttonBGProperty, value); } } public static readonly DependencyProperty buttonBGProperty = DependencyProperty.Register("buttonBG", typeof(SolidColorBrush), typeof(DataPanel), null);
usercontrol.xaml
<Button ... Background="{Binding buttonBG}">
これはうまくいくと思っていましたが、背景は私がウィンドウリソースに設定したものではありません。
私は間違っていますか?
あなたは 'buttonBG =" {Binding Source = {StaticResource theBG}} "を' buttonBG = "{StaticResource theBG}" 'に単純化することができます。 –
'SolidColorBrush'は不必要に特殊なもので、' Brush'をプロパティ型として使うだけです。 –