Fazer uma PerguntaFazer uma Pergunta
 

RespondidoUserControls

  • quinta-feira, 24 de julho de 2008 10:42vbturbo Medalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuário
     Contém Código
    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





Respostas

  • quinta-feira, 24 de julho de 2008 18:15Bragi Medalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuário
     Respondido
    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
    • Marcado como Respostavbturbo quinta-feira, 24 de julho de 2008 19:28
    •  

Todas as Respostas

  • quinta-feira, 24 de julho de 2008 15:13Bragi Medalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuário
     Contém Código
    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).
  • quinta-feira, 24 de julho de 2008 18:05vbturbo Medalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuário
     
    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
  • quinta-feira, 24 de julho de 2008 18:15Bragi Medalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuário
     Respondido
    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
    • Marcado como Respostavbturbo quinta-feira, 24 de julho de 2008 19:28
    •