2010-05-27 3 views
0

J'ai un gridview étaient des colonnes je définis, comme ça ...WPF notifieront les modifications sur l'objet

<GridViewColumn.CellTemplate> 
    <DataTemplate> 
     <TextBlock Text="{Binding MyProp}" /> 
    </DataTemplate> 
</GridViewColumn.CellTemplate> 

Je lie mon gridview à une collection et implemts INotifyPropertyChanged dans la MyProp propriété. Cela fonctionne bien et tous les changements de MyProp sont reflétés dans le gridview.

Si j'ajoute une autre colonne qui est liée à l'objet lui-même, je ne reçois aucune notification/mise à jour. Mon code ...

<GridViewColumn.CellTemplate> 
    <DataTemplate> 
     <TextBlock Text="{Binding Converter={StaticResource myConverter}}"/> 
    </DataTemplate> 
</GridViewColumn.CellTemplate> 

Je pense que je besoin de quelque chose comme INotifyPropertyChanged pour l'objet, mais je ne sais pas comment faire. Aucune suggestion?

Répondre

5

Oui, l'instance elle-même ne change jamais - seulement ses propriétés.

Votre convertisseur repose vraisemblablement sur un ensemble de propriétés de l'objet auquel vous vous êtes lié? Si c'est le cas, vous pouvez utiliser MultiBinding et changer votre convertisseur en IMultiValueConverter. Vous pouvez ensuite lier à toutes les propriétés dépendantes qui peuvent provoquer la mise à jour du TextBlock.

+0

Merci beaucoup, c'est exactement ce dont j'ai besoin. –

0

Faire l'objet impletment l'interface INotifyPropertyChanged

Voici un exemple de MSDN

public class DemoCustomer : INotifyPropertyChanged 
{ 
// These fields hold the values for the public properties. 
private Guid idValue = Guid.NewGuid(); 
private string customerName = String.Empty; 
private string companyNameValue = String.Empty; 
private string phoneNumberValue = String.Empty; 

public event PropertyChangedEventHandler PropertyChanged; 

private void NotifyPropertyChanged(String info) 
{ 
    if (PropertyChanged != null) 
    { 
     PropertyChanged(this, new PropertyChangedEventArgs(info)); 
    } 
} 

// The constructor is private to enforce the factory pattern. 
private DemoCustomer() 
{ 
    customerName = "no data"; 
    companyNameValue = "no data"; 
    phoneNumberValue = "no data"; 
} 

// This is the public factory method. 
public static DemoCustomer CreateNewCustomer() 
{ 
    return new DemoCustomer(); 
} 

// This property represents an ID, suitable 
// for use as a primary key in a database. 
public Guid ID 
{ 
    get 
    { 
     return this.idValue; 
    } 
} 

public string CompanyName 
{ 
    get {return this.companyNameValue;} 

    set 
    { 
     if (value != this.companyNameValue) 
     { 
      this.companyNameValue = value; 
      NotifyPropertyChanged("CompanyName"); 
     } 
    } 
} 
public string PhoneNumber 
{ 
    get { return this.phoneNumberValue; } 

    set 
    { 
     if (value != this.phoneNumberValue) 
     { 
      this.phoneNumberValue = value; 
      NotifyPropertyChanged("PhoneNumber"); 
     } 
    } 
} 
} 
+0

Il a déjà implémenté INotifyPropertyChanged, mais il veut essentiellement notifier "this" qui ne fonctionnera pas. – JustABill

Questions connexes