Search code examples
c#wpfuser-controlscontentcontroldockpanel

Contentcontrol or dockpanel for navigation wpf


I use a Contentcontrol to show the user controls of the program, Now there is a problem for me to close the user controls After searching, I found an example that The user controls is loaded on a DockPanel
Now my questions:

  1. What is the difference between these two controls? (Dockpanel vs ContentControl)

  2. Is it okay to use this control(dockpanel) Instead of Contentcontrol to display application user controls?

  3. Is there a similar code for Contentcontrol?

    ucChild ChildWindow = new ucChild();  
    ChildWindow.ParentControl = this.UIPanel;  
    UIPanel.Children.Clear();  
    UIPanel.Children.Add(ChildWindow);
    

Solution

  • Standard disclaimer for people coding WPF like it is WinForms: First off; direct UI manipulation like this is a bad idea. You should be modifying a view model and allowing the binding system to update the UI. Use the MVVM pattern; WPF will work for you instead of against you

    To your actual questions:

    1. Everything. I mean; they both inherit from FrameworkElement but that's about it in terms of commonality.

      • A DockPanel is as the name suggests, a Panel. That is; it controls the layout and sizing of one or more child elements. Specifically, DockPanel is good at situations like the following: you want an element to use up a full column of width, then another span the top (except for the previous element) and have the last element fill the remaining space.
      • A ContentControl is basically a placeholder, its purpose is to expose a settable (and most importantly, bindable) Content property that you can stuff another control into. Even better; you can put an actual object there and use a DataTemplate to control the display (this approach would conform to MVVM).
    2. You can't really replace one with the other, see above

    3. No. ContentControl is not a Panel and so does not have the Children property.