2010-05-27 55 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