Ich versuche, ein WPF UserControl zu erstellen, das 2 Schaltflächen enthält. Ich verwende dieses UserControl in einem Fenster und wende einen Window.Resource-Wert an, um den Hintergrund einer Schaltfläche innerhalb des Benutzersteuerelements festzulegen.WPF UserControl Eigenschaft Bindung
Zur Zeit habe ich:
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}">
Ich habe erwartet, dass dies funktioniert, aber der Hintergrund ist nicht der, den ich im Fenster Ressource festgelegt.
Was mache ich falsch?
Sie können 'buttonBG =" {Binding Source = {StaticResource theBG}} "' zu 'buttonBG =" {StaticResource theBG} "' vereinfachen, die Bindung tut nichts. –
'SolidColorBrush' in unnötig spezifischen, würde nur' Brush' als Eigenschaftstyp verwenden. –