Domanda

Sto cercando di registrare 3 proprietà di dipendenza su una finestra per controllarne la formattazione. Ho guardato più volte il codice, ma devo mancare qualcosa.

public static readonly DependencyProperty TextColorProperty = DependencyProperty.Register("TextColor", typeof(Color), typeof(WinStickyFingers), new PropertyMetadata(Colors.White));
public Color TextColor {
    get { return (Color)base.GetValue(TextColorProperty); }
    set { base.SetValue(TextColorProperty, value); }
}

public static readonly DependencyProperty BackgroundColorProperty = DependencyProperty.Register("BackgroundColor", typeof(Color), typeof(WinStickyFingers), new PropertyMetadata(Colors.Black));
public Color BackgroundColor {
    get { return (Color)base.GetValue(BackgroundColorProperty); }
    set {
        base.SetValue(BackgroundColorProperty, value);
    }
}    
<TextBlock DockPanel.Dock="Top" Text="{Binding Name}" Foreground="{Binding TextColor,Converter={StaticResource DebugConverter}}" Background="{Binding Path=BackgroundColor}" />

Sto usando il metodo di debug di Bea Stollnitz ma il mio punto di interruzione non è nemmeno attivato.

È stato utile?

Soluzione

Che cos'è il DataContext del TextBlock ? Come fa a sapere che dovrebbe legarsi alle proprietà sulla tua Window ?

Devi impostare DataContext sull'istanza Window oppure impostare Source (o RelativeSource , o ElementName ) sulle tue associazioni. Tutte queste proprietà esistono come mezzo per risolvere l'oggetto associato per il tuo Binding . DataContext è un fallback se nessuno degli altri è impostato, ma immagino che non sia stato impostato neanche tu.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top