發問發問
 

已答覆UserControls

  • Thursday, 24 July, 2008 10:42vbturbo 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     包含代碼
    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





解答

  • Thursday, 24 July, 2008 18:15Bragi 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     已答覆
    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
    • 已標示為解答vbturbo Thursday, 24 July, 2008 19:28
    •  

所有回覆

  • Thursday, 24 July, 2008 15:13Bragi 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     包含代碼
    From your usercontrol, you can get the visual parent, which is the canvas you put it in.  To get the visual parent, use:
    VisualTreeHelper.GetParent(this); 
    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).
  • Thursday, 24 July, 2008 18:05vbturbo 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     
    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
  • Thursday, 24 July, 2008 18:15Bragi 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     已答覆
    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
    • 已標示為解答vbturbo Thursday, 24 July, 2008 19:28
    •