My goal is to bind to a set of objects in WPF. I first tried to use generics but horribly failed at it. Since I can’t cast generic properties to INotifyProperty interfaces, to hook up the changedevent.
So I came up with this class to use as an object in my BindingList. ( Yes, I need those to notify me when an object within the list changes ). So I need your opinion, improvements on my code.
public class GPair : ObservableObject
{
public GPair()
{
}
private ObservableObject _first;
public ObservableObject First
{
get
{
return this._first;
}
set
{
this._first = value;
((ObservableObject)value).PropertyChanged +=new PropertyChangedEventHandler(First_PropertyChanged);
RaisePropertyChanged("First");
}
}
private ObservableObject _second;
public ObservableObject Second
{
get
{
return this._second;
}
set
{
this._second = value;
((ObservableObject)value).PropertyChanged += new PropertyChangedEventHandler(Second_PropertyChanged);
RaisePropertyChanged("Second");
}
}
private void First_PropertyChanged(object sender, PropertyChangedEventArgs args)
{
RaisePropertyChanged("First");
}
private void Second_PropertyChanged(object sender, PropertyChangedEventArgs args)
{
RaisePropertyChanged("Second");
}
}
The ObservableObject is just a helperclass that implements the INotifyPropertyChanged.
This piece of code allows me couple two objects in a binding… The Binding itself would look like this:
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=First.ObjectPropertie}" />
<TextBlock Text="{Binding Path=Second.ObjectPropertie}" />
</StackPanel>
Any opinions or improvements??
Kind regards!
I don’t fully get what you are trying to achieve, but the GPair class looks odd
What about having something like this?
HTH