views:

113

answers:

2

I have a gridview were I define some columns, like this...

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

I bind my gridview to a collection and implemts INotifyPropertyChanged in the property MyProp. This works well and any changes of MyProp are reflected to the gridview.

If I add another column that is bound to the object itself I dont get any notifications/updates. My code...

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

I think I need something like INotifyPropertyChanged for the object but I have no idea how to do this. Any suggestions?

A: 

Make the object impletment the interface INotifyPropertyChanged

Here is an example from 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");
        }
    }
}
}
alejandrobog
He's already implemented INotifyPropertyChanged, but he basically wants to notify "this" which won't work.
JustABill
+2  A: 

Yes, the actual instance itself never changes - only its properties.

Presumably your converter relies on a bunch of properties from the object you've bound to? If so, you could use a MultiBinding and change your converter to an IMultiValueConverter. Then you can bind to all the dependent properties that might cause the TextBlock to update.

Matt Hamilton
Thanks a lot, this is excatly what I need.
Erik Z