Formula una domandaFormula una domanda
 

Con rispostaUserControls

  • giovedì 24 luglio 2008 10.42vbturbo Medaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     Contiene codice
    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





Risposte

  • giovedì 24 luglio 2008 18.15Bragi Medaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     Con risposta
    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
    • Contrassegnato come rispostavbturbo giovedì 24 luglio 2008 19.28
    •  

Tutte le risposte

  • giovedì 24 luglio 2008 15.13Bragi Medaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     Contiene codice
    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).
  • giovedì 24 luglio 2008 18.05vbturbo Medaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     
    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
  • giovedì 24 luglio 2008 18.15Bragi Medaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     Con risposta
    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
    • Contrassegnato come rispostavbturbo giovedì 24 luglio 2008 19.28
    •