.NET Framework Developer Center >
.NET Development Forums
>
Windows Presentation Foundation (WPF)
>
UserControls
UserControls
- Hi
I have a window with a btnLoadUC and canvas where i add the usercontrol's children.
Dim uc As New myUserControl
Canvas.Children.Add(uc)
Works fine
now in my usercontrol i have a close button , so how do i call the parent control to remove the uc from the
canvas?
Thanks
Answers
- No there isn't. here's the problem: a UserControl can visually be 'owned' by any number of different controls: it could be the Content of a Button, Popup, MenuItem, it could be put on a ToolBar, a Panel, in the template of an itemscontrol. Sometimes it could close, sometimes it can't. You should try to look at it from the other side of the equation: if you need to manage several objects on a canvas that need to be dynamically added and removed, use an ItemsControl instead. Bind this to an observable collection that contains your userControls (or better: an observableCollection that contains data objects + use a DataTemplate that contains your userControl). If you add and remove items from this list, it will automatically be updated in your listbox. To use a canvas instead of a StackPanel, change the ItemsPanelTemplate property of the ItemsControl and assign it a Canvas.
To correctly set Canvas.Left, .... you should assign an ItemContainerStyle to the ItemsControl which should create a binding between the Canvas.XXX properties and your Data properties.
Hope this helps.
Bragi- Marked As Answer byvbturbo Thursday, July 24, 2008 7:28 PM
All Replies
- From your usercontrol, you can get the visual parent, which is the canvas you put it in. To get the visual parent, use:VisualTreeHelper is a helper class that allows you to walk the visual tree. The visual tree is a tree built up of UI elements, ordered in the way that they are currently visible (rendered). You also have the logicalTree, which represents the tree structure of your xaml (this can be different from the visual tree).
VisualTreeHelper.GetParent(this);
- Hi Bragi
Thanks for reply.
Yes im now able to get the parent control.
Though is there an easier way to unload the control from it self ?
like this in my control this.close();
Thanks
- No there isn't. here's the problem: a UserControl can visually be 'owned' by any number of different controls: it could be the Content of a Button, Popup, MenuItem, it could be put on a ToolBar, a Panel, in the template of an itemscontrol. Sometimes it could close, sometimes it can't. You should try to look at it from the other side of the equation: if you need to manage several objects on a canvas that need to be dynamically added and removed, use an ItemsControl instead. Bind this to an observable collection that contains your userControls (or better: an observableCollection that contains data objects + use a DataTemplate that contains your userControl). If you add and remove items from this list, it will automatically be updated in your listbox. To use a canvas instead of a StackPanel, change the ItemsPanelTemplate property of the ItemsControl and assign it a Canvas.
To correctly set Canvas.Left, .... you should assign an ItemContainerStyle to the ItemsControl which should create a binding between the Canvas.XXX properties and your Data properties.
Hope this helps.
Bragi- Marked As Answer byvbturbo Thursday, July 24, 2008 7:28 PM


