2010-01-21 3 views
0

j'ai ListBox personnalisé déclaré en XAML:WPF: Reliure ListBox personnalisée et liste <T>: PropertyChanged est toujours nul

<ListBox x:Name="uicMDSQonfServer"> 
    <ListBox.ItemTemplate> 
    <DataTemplate> 
     <StackPanel Orientation="Horizontal" 
        Margin="0,5,0,5"> 
     <CheckBox IsChecked="{Binding RelativeSource={TemplatedParent}, 
             Path=Activated}" /> 
     <ContentPresenter Content="{Binding RelativeSource={TemplatedParent}, 
            Path=Content}"/> 
     </StackPanel> 
    </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 

J'ai besoin de dsiplay et Interop avec liste générique, où T est:

public class QonfServer: QonfBase, INotifyPropertyChanged 
{ 
     private string ip; 
     private bool activated; 

     public string Ip { 
      get { return ip; } 
     } 

     public bool Activated 
     { 
      get { return activated; } 
      set 
      { 
       if (activated == value) 
        return; 

       activated = value; 
       if (PropertyChanged != null) 
        PropertyChanged(this, new PropertyChangedEventArgs("Activated")); 
      } 
     } 

     #region INotifyPropertyChanged Members 
     public event PropertyChangedEventHandler PropertyChanged; 
     #endregion 
    } 

QonfBase est assez simple classe de base:

public class QonfBase 
{ 
     private int id; 
     public int ID { get; set; } 
} 

Quand je tourne activ Par programmation, la case à cocher ne change pas d'état. Débogage: PropertyChanged = null. Quelqu'un sait-il, qu'est-ce qui est incorrect?

+0

vérifiez-vous la fenêtre de sortie? existe-t-il des erreurs de liaison? –

+0

Il n'y a pas d'erreur de liaison. – Pavel

Répondre

1

Un problème évident se pose: TemplatedParent est à utiliser avec ControlTemplate s. Puisque vous utilisez un DataTemplate, cela devrait fonctionner:

<CheckBox IsChecked="{Binding Activated}" /> 
<ContentPresenter Content="{Binding Content}"/> 

Je n'ai pas remarqué de problèmes avec le C#.

+0

Merci, Ray! J'ai enlevé RelativeSource explicite et maintenant son travail! – Pavel