2010-07-04 8 views
0

Pourquoi DataBinding ne fonctionne pas?Pourquoi DataBinding ne fonctionne pas?

<TextBox Text="{Binding Path=local:MainWindow.SearchPlayer, 
    Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" /> 

ceci est ma classe:

public partial class MainWindow : Window 
    { 
     private Store store = new Store(); 
     private string _searchPlayer; 
     public string SearchPlayer 
     { 
      get 
      { 
       return _searchPlayer; 
      } 
      set 
      { 
       _searchPlayer = value; 
       if(_searchPlayer!="") 
       { 
        ACT.DataContext = store.SearchedPlayers 
         .Where(x => x.StartsWith(_searchPlayer)).ToList(); 
       } 
       else 
       { 
        ACT.DataContext = store.Last10SearchedPlayers; 
       } 
      } 
     } 

     public MainWindow() 
     {...............} 

Je mets point d'arrêt sur setter SearchPlayer mais il n'a jamais travaillé.

+1

Je n'ai jamais vu la syntaxe de liaison 'Path = local: MainWindow.SearchPlayer' avant. D'où vient-il? – Gabe

Répondre

2

Je ne pense pas Binding Path=local:MainWindow.SearchPlayer fonctionnera, car MainWindow est une classe, pas une instance. Cela pourrait fonctionner si SearchPlayer était statique, mais je ne pense pas que vous le vouliez.

Il suffit d'utiliser Binding Path=SearchPlayer et assurez-vous que le DataContext est correctement réglé. Dans le constructeur MainWindow: this.DataContext=this; (varie en fonction de l'emplacement de la zone de texte).

et notez que MainWindow doit implémenter l'interface INotifyProperty et le poseur de SearchPlayer devrait appeler OnPropertyChanged.

3
<TextBox Text="{Binding Path=SearchPlayer, 
         RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:MainWindow}} 
         Mode=TwoWay, 
         UpdateSourceTrigger=PropertyChanged}" /> 
+0

Cool, mais est-ce vraiment le moyen le plus approprié ici? –

Questions connexes