benjamin second
benjamin second

Reputation: 1

Right way to put few UserControl Components on a Window

In any large application I have this problem:

I am building window look and I want it to have for example menu on left, some content in the middle etc.

Now if I want to reuse some of this stuff (most of it) or simply I don't want to keep it all in one xaml file how do I do that? Till now I used to do this like this:

  1. I create UserControl + ViewModel for those components (usually I need to split main view to two or three components)
  2. In main Window (xaml) i put:

    <DockPanel>
            <ContentControl Content="{Binding Menu}" DockPanel.Dock="Left"/>
            <ContentControl Content="{Binding MainStuff}"/>
    </DockPanel>
    
  3. In main WindowViewModel i put:

    Menu = new MenuViewModel();
    MainStuff = new MainStuffViewModel();
    

And sometimes those VModels have to reffer to MainViewModel but rarely.

This approach is not very good couse I can't see my design in main window xaml editor (sometimes I can see it, I don't know, visual probably tries to do binding before compilation but sometimes it fails, I don't want to think about it :))

What is pro approach here?

Upvotes: 0

Views: 132

Answers (2)

ThomasAndersson
ThomasAndersson

Reputation: 1904

Do you have a corresponding MenuView for your MenuViewModel?

In that case you can replace your ContentControl like the following:

<DockPanel>
        <prefix:MenuView DataContext="{Binding Menu}" DockPanel.Dock="Left"/>
        <prefix:MainStuffView DataContext="{Binding MainStuff}"/>
</DockPanel>

This way you get design view of your MenuView- and MainStuffView-usercontrols

Upvotes: 1

anivas
anivas

Reputation: 6567

Large application need to move away from it's monolithic form to be reusable, composable - you are looking for Prism. Create regions for your sections and convert your use control to views and inject them.

Upvotes: 0

Related Questions