Actually, I asked myself this question a long time ago, but I could not find the answer.
If it is necessary on one form to display different panels on the same place (such as TabControl without visualized tabs), how should this be correctly implemented? Interested in competent implementation on WinForms and WPF .


Before that, I used it only in WinForms in the following variations:

A user control, inherited from tabcontrol, in which an event was intercepted, was downloaded from the net, and the tabs were not visualized. In the future, switching tabs was carried out through other controls.

Overlayed each other panels, and periodically changed their visibility. Hemorrhagic is still the same.

    5 answers 5

    As for WinForms, I will not say, but for WPF there are, as @Spawn correctly notes, a million different solutions, from switching visibility to restyling ItemsControl 'a. Choose the one that fits best with the semantics of your task.

    For example, I often use this solution: I just define

     <ContentPresenter Content="{Binding VM}"/> 

    and substitute VM when needed. VM mapping is set, of course, through DataTemplate .

    • I like your version with Binding VM, I will try) - svinopapka
    • And how to do it correctly? For example, I suppose that I need to define all the individual “panels” in different xaml files, perhaps in the form of a Page, and then somehow connect them to the main window in this very Binding, but how to do it? - svinopapka
    • @svinopapka: the required panels must be defined in the DataTemplate 'ah for the type of VM (that is, the object of your business logic) that they will represent. They are usually connected to global resources. - VladD

    Perhaps there is no “correct” solution in this matter. It seems to me that in each case there will be some nuances.

    For WinForms, see for example this example .

    And for WPF, is there a more or less standard solution?

    • Thank you, but I am not more interested in wizards, but simply switching between panels, which can be completely unrelated to each other. - svinopapka
    • This will then be just a switch :-) - Spawn

    I will tell you how I did it once for WinForms. Immediately I will warn you that this is all the same switching visibility for the n-th number of panels superimposed on each other. RadioButtons are used as switches. Each RadioButton knows which panel it should show (for example, you can save a link to the required panel in the Tag property). Also saved a link to the panel that is currently displayed. Then the approximate code of the radio button handler will be as follows:

     private Panel selectedPanel; // Ссылка на активную панель ... private void SelectPanel(object sender, EventArgs e) { var radioButton = sender as RadioButton; if (radioButton != null && radioButton.Checked) { if (selectedPanel != null) selectedPanel.Hide(); selectedPanel = radioButton.Tag as Panel; if (selectedPanel != null) selectedPanel.Show(); } } 

    In my opinion, not such a hemorrhoid, if we consider that more code is not needed. But there is an inconvenience when working with this "stack" of panels in the form designer.

    • For some reason, it's really simple, but in general, because The work is carried out exclusively in the visual editor - does not roll at all) - svinopapka

    As for winForms, there is such a thing as GroupBox, and by hiding it, you hide all the child elements, as for me it is.

      Use WPF Navigation . Create Page pages. Switch them to Frame .

      Here are some examples:
      Page + Frame
      DialogWizard from MSDN