views:

192

answers:

1

Is there an object or interface I can use to make an auto-updating object, and have those updates filter through to the UI in WPF?

Basically I'm looking for something like this:

<TextBlock Text="{Binding Source={StaticResource myClass}, Path=DataObject}" />

public class MyClass
{
    public AutoUpdatingObject DataObject { get; set; }
}

public class AutoUpdatingObject: ???
{
    ???
}

No I can't make MyClass implement INotifyPropertyChanged. I need the AutoUpdatingObject class to be able to notify WPF of changes.

+1  A: 

Implementing INotifyPropertyChanged is not the only way to update bindings, you can also use events without implementing an interface.

Using a CLR Class as the Binding Source Object

If you do not implement INotifyPropertyChanged, then you must arrange for your own notification system to make sure that the data used in a binding stays current. You can provide change notifications by supporting the PropertyChanged pattern for each property that you want change notifications for. To support this pattern, you define a PropertyNameChanged event for each property, where PropertyName is the name of the property. You raise the event every time the property changes.

In your case MyClass would look like this:

public class MyClass
{
 private AutoUpdatingObject dataObject;
 public AutoUpdatingObject DataObject
 {
  get { return dataObject; }
  set { dataObject = value; DataObjectChanged(this, new EventArgs()); }
 }

 public event EventHandler DataObjectChanged = delegate { };
}
Bubblewrap