質問する質問する
 

回答済みUserControls

  • 2008年7月24日 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





回答

  • 2008年7月24日 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 2008年7月24日 19:28
    •  

すべての返信

  • 2008年7月24日 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).
  • 2008年7月24日 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
  • 2008年7月24日 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 2008年7月24日 19:28
    •