2009-08-28 14 views
5

Ho una proprietà di dipendenza stringa (SearchText), quando aggiornata, è necessario aggiornare una proprietà di dipendenza della raccolta (risultati).
mia collezione dp:Aggiornamento di una proprietà di dipendenza da un'altra

public IEnumerable<string> Results{ 
    get { return (IEnumerable<string>) GetValue(ResultsProperty); } 
    set { SetValue(ResultsProperty, value); } 
} 
public static readonly DependencyProperty ResultsProperty= 
DependencyProperty.Register("Results", typeof(IEnumerable<string>), typeof(MainWindowVM), new UIPropertyMetadata(new List<string>())); 

ho provato questo senza fortuna. ho inserito un breakpoint nella riga Results = .... e non è mai stato colpito.

public string SearchText{ 
    get { return (string) GetValue(SearchTextProperty); } 
    set { 
    Results = 
      from T in Tree.GetPeople(value) 
      select T.FullName; 
    SetValue(SearchTextProperty, value); 
    } 
} 
public static readonly DependencyProperty SearchTextProperty= 
DependencyProperty.Register("SearchText", typeof(string), typeof(MainWindowVM), new UIPropertyMetadata("")); 

XAML:

<TextBox DockPanel.Dock="Top" Text="{Binding SearchValue, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />

<ListBox DockPanel.Dock="Top" ItemsSource="{Binding NameResults}" SelectedItem="{Binding Search}" />

risposta

7

Quando si imposta una proprietà di dipendenza in XAML o attraverso un legame, il tempo di esecuzione sarà sempre bypassare l'alias proprietà di istanza e direttamente chiamare GetValue e SetValue . È per questo motivo che il setter dell'istanza non viene invocato.

Ciò che si potrebbe desiderare di prendere in considerazione è la registrazione di un metodo con la proprietà di dipendenza che verrà richiamata quando la proprietà cambia. Questo è più facile quando si crea lo PropertyMetadata per la proprietà di dipendenza.

Credo che l'esempio seguente faccia quello che stai cercando di fare. Nell'esempio, la mia classe ha due proprietà di depenzionalità alias come Primo e Secondo. Quando imposto il valore per First, viene richiamato il gestore delle modifiche e imposto il valore di Second.

public class DependencyPropertyTest : DependencyObject 
{ 
    public static readonly DependencyProperty FirstProperty; 
    public static readonly DependencyProperty SecondProperty; 

    static DependencyPropertyTest() 
    { 
    FirstProperty = DependencyProperty.Register("FirstProperty", 
                typeof(bool), 
                typeof(DependencyPropertyTest), 
                new PropertyMetadata(false, FirstPropertyChanged)); 

    SecondProperty = DependencyProperty.Register("SecondProperty", 
               typeof(string), 
               typeof(DependencyPropertyTest), 
               new PropertyMetadata(null)); 
    } // End constructor 

    private bool First 
    { 
    get { return (bool)this.GetValue(FirstProperty); } 
    set { this.SetValue(FirstProperty, value);  } 

    } // End property First 

    private string Second 
    { 
    get { return (string)this.GetValue(SecondProperty); } 
    set { this.SetValue(SecondProperty, value);   } 

    } // End property Second 

    private static void FirstPropertyChanged(DependencyObject dependencyObject, 
              DependencyPropertyChangedEventArgs ea) 
    { 
    DependencyPropertyTest instance = dependencyObject as DependencyPropertyTest; 

    if (instance == null) 
    { 
     return; 
    } 

    instance.Second = String.Format("First is {0}.", ((bool)ea.NewValue).ToString()); 

    } // End method FirstPropertyChanged 
} // End class DependencyPropertyTest 

Spero che questo aiuti.

+0

-1, questo si romperà vincolante per la proprietà 'secondo' perché ImpostaValore verrà utilizzata al posto di SetCurrentValue (vedi http://stackoverflow.com/ domande/4230698/cosa-la-differenza-tra-dipendenza-proprietà-setvalue-SetCurrentValue). – Benlitz

+3

Vero oggi, ti concederò. Tuttavia, al momento in cui questa risposta è stata scritta, .NET Framework 3.5 era la versione più recente che non aveva un membro SetCurrentValue. (rif: https://msdn.microsoft.com/en-us/library/system.windows.dependencyobject_members(v=vs.90).aspx) Vale la pena un commento o una risposta aggiuntiva per modernizzare, ma IMHO non garantisce un voto negativo. –

5

Come ho commentato, la risposta attualmente accettata è corretta nel principio, tranne che DEVE utilizzare il metodo SetCurrentValue invece di eseguire un semplice compito. Vedi this answer per ulteriori spiegazioni a riguardo.

Qui è lo stesso codice con questa correzione:

public class DependencyPropertyTest : DependencyObject 
{ 
    public static readonly DependencyProperty FirstProperty; 
    public static readonly DependencyProperty SecondProperty; 

    static DependencyPropertyTest() 
    { 
    FirstProperty = DependencyProperty.Register("FirstProperty", 
                typeof(bool), 
                typeof(DependencyPropertyTest), 
                new PropertyMetadata(false, FirstPropertyChanged)); 

    SecondProperty = DependencyProperty.Register("SecondProperty", 
               typeof(string), 
               typeof(DependencyPropertyTest), 
               new PropertyMetadata(null)); 
    } // End constructor 

    private bool First 
    { 
    get { return (bool)this.GetValue(FirstProperty); } 
    set { this.SetValue(FirstProperty, value);  } 

    } // End property First 

    private string Second 
    { 
    get { return (string)this.GetValue(SecondProperty); } 
    set { this.SetValue(SecondProperty, value);   } 

    } // End property Second 

    private static void FirstPropertyChanged(DependencyObject dependencyObject, 
              DependencyPropertyChangedEventArgs ea) 
    { 
    DependencyPropertyTest instance = dependencyObject as DependencyPropertyTest; 

    if (instance == null) 
    { 
     return; 
    } 

    // SetCurrentValue should be used here! 
    instance.SetCurrentValue(SecondProperty, 
     String.Format("First is {0}.", ((bool)ea.NewValue).ToString()); 

    } // End method FirstPropertyChanged 
} // End class DependencyPropertyTest 
+0

Dovrebbe essere un commento! –

Problemi correlati