WPF通知对象上的更改

WPF通知对象上的更改,wpf,Wpf,我有一个gridview,我定义了一些列,像这样 <GridViewColumn.CellTemplate> <DataTemplate> <TextBlock Text="{Binding MyProp}" /> </DataTemplate> </GridViewColumn.CellTemplate> 我将gridview绑定到集合,并在属性MyProp中执行INotifyPropertyCh

我有一个gridview,我定义了一些列,像这样

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

我将gridview绑定到集合,并在属性MyProp中执行INotifyPropertyChanged。这很有效,MyProp的任何更改都会反映到gridview中

如果我添加另一个绑定到对象本身的列,我不会收到任何通知/更新。我的代码

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


我想我需要像INotifyPropertyChanged这样的对象,但我不知道如何做到这一点。有什么建议吗?

使对象实现接口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");
        }
    }
}
}

使对象实现成为接口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");
        }
    }
}
}

是的,实际实例本身永远不会改变——只会改变其属性


您的转换器可能依赖于绑定到的对象的一组属性?如果是这样,您可以使用多重绑定并将转换器更改为IMultiValueConverter。然后,您可以绑定到可能导致TextBlock更新的所有依赖属性。

是的,实际实例本身从不更改,只更改其属性


您的转换器可能依赖于绑定到的对象的一组属性?如果是这样,您可以使用多重绑定并将转换器更改为IMultiValueConverter。然后,您可以绑定到所有可能导致文本块更新的依赖属性。

他已经实现了INotifyPropertyChanged,但他基本上想通知“this”,这不起作用。他已经实现了INotifyPropertyChanged,但他基本上想通知“this”,这不起作用。非常感谢,这正是我所需要的。非常感谢,这正是我所需要的。