OmidH

Hi, I am working on a custom UserControl, but I am not sure how to use the INotifyPropertyChanged Interface.

I have a Class STElement : UserControl, INotifyPropertyChanged where I implemented the Interface as following:

Code Block

public bool STExpanded
{
get
{
return _expanded;
}

set
{
_expanded = value;
OnPropertyChanged("STExpanded");
}
}

...

protected void OnPropertyChanged(string p)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs(p));
}


On debuging the OnPropertyChanged(string p) the handler is all the time over null.
Now I have another Class ( public class SphericTreeCanvas : Canvas) and I override the OnPropertyChanged Method:


Code Block

protected override void OnPropertyChanged(System.Windows.DependencyPropertyChangedEventArgs e)
{
Console.WriteLine(" => " + e.Property.Name);
if (e.Property.Name.Equals("STExpanded"))
{
paintNodes(root, mainPanel);
Console.WriteLine("OnPropertyChanged");
}
base.OnPropertyChanged(e);
}


to see which properties are changed, but I never see STExpanded listed in there....

What do I do wrong


thanks in advance,

Omid


Re: Windows Presentation Foundation (WPF) INotifyPropertyChanged

Drew Marsh

Since you're writing a WPF Control, you should be defining dependency properties for the properties that you're going to expose and then simply defining CLR wrapper properties around those. The nofitication of property changes will then take place using the intrinsic dependency object/property system. Basically what that means is, there's no need for you to do any INotifyPropertyChanged work yourself.

Try this:

Code Block

public class STElement : UserControl

{
public static readonly DependencyProperty STExpandedProperty = DependencyProperty.Register("STExpanded", typeof(bool), typeof(STElement));

public bool STExpanded
{

get

{

return (bool)base.GetValue(STElement.STExpandedProperty);

}

set

{

base.SetValue(STElement.STExpandedProperty, value);

}

}
}

You should also consider not inheriting from UserControl, but maybe just ContentControl or Control depending on what the goals are for the design and reusability of STElement. More on that subject here in the SDK.

HTH,
Drew





Re: Windows Presentation Foundation (WPF) INotifyPropertyChanged

OmidH

Hi Drew,
thank you for the post. I tried it, but it does not work for me.
I am not able to catch the propertyChange in OnPropertyChanged in my Canvas.

thx,
Omid




Re: Windows Presentation Foundation (WPF) INotifyPropertyChanged

Drew Marsh

Ok, I see what you're trying to do now. So the problem is, overriding OnPropertyChanged is not how you catch the changing of properties on other controls as changes don't magically propagate up the hierarchy. Property changes must be caught by hooking the PropertyChanged event of the instance of each control.

Let me see if I understand what you're trying to do, you want to know when a certain property of any child of your custom canvas changes and, in response to that, do some kind of processing. Is that correct

Theoretically you could continue doing what you're doing with implementing INotifyPropertyChanged, then as children are added to your Canvas you hook their change notification and see if it's the event you're interested in, but it's going to create a lot of overhead and isn't really the "right" approach in the WPF control world.

Do me a favor and confirm that I understand what you're trying to do and fill in more details if possible and then I can try and suggest a good approach for it.

HTH,
Drew





Re: Windows Presentation Foundation (WPF) INotifyPropertyChanged

OmidH

Hi Drew,

you understand it right.
What I want to do is to visualize an semantic Tree, so I decided to implement 3 classes (Root, Node, Leaf) each derive from STElement.

public class STElement : UserControl
|
|------------------------------------------------------------------------|-------------------------------------------------------------------------|
public partial class STLeaf : STElement public partial class STNode : STElement public partial class STRoot :
STElement


Then I have a Class public class SphericTreeCanvas : Canvas for the visualisation of the Tree. Now the Problem is, when I doubleclick a Node or Root I would like to expand or collapse the Children of the node. Actually that's all the problem. I tried to override the OnRender Method but it is not working, than I found the OnPropertyChanged Event tried it with...


regards,
Omid