2010-05-27 30 views
0

我有一個GridView是我定義了一些列的,是這樣的...WPF通知變動對對象

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

我我的GridView控件綁定到一個集合,implemts在屬性MyProp INotifyPropertyChanged的。這效果很好,MyProp的任何變化都反映到gridview中。

如果我添加另一列綁定到對象本身,我沒有得到任何通知/更新。我的代碼...

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

我想我需要像INotifyPropertyChanged這樣的對象,但我不知道如何做到這一點。有什麼建議麼?

回答

5

是,實際的實例本身永遠不會改變的例子 - 只是它的屬性。

推測你的轉換器依賴於你綁定的對象的一堆屬性?如果是這樣,您可以使用MultiBinding並將您的轉換器更改爲IMultiValueConverter。然後,您可以綁定到可能導致TextBlock更新的所有依賴屬性。

+0

非常感謝,這是我需要的激動人心的。 – 2010-05-27 07:17:28

0

使對象impletment接口INotifyPropertyChanged的

下面是從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

他已經實施INotifyPropertyChanged,但他基本上想通知「這個」哪個行不通。 – JustABill 2010-05-27 06:16:59