在窗口上放置少量 UserControl 组件的正确方法 [英] Right way to put few UserControl Components on a Window

查看:38
本文介绍了在窗口上放置少量 UserControl 组件的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在任何大型应用程序中,我都会遇到这个问题:

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.

现在,如果我想重用这些东西中的一些(大部分)或者只是不想将它们全部保存在一个 xaml 文件中,我该怎么做?直到现在我曾经这样做过:

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. 我为这些组件创建了 UserControl + ViewModel(通常我需要将主视图拆分为两个或三个组件)
  2. 在主窗口 (xaml) 中,我输入:

  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>

  • 在主 WindowViewModel 我放:

  • In main WindowViewModel i put:

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

  • 有时那些 VModel 不得不参考 MainViewModel,但很少.

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

    这种方法不是很好,因为我在主窗口 xaml 编辑器中看不到我的设计(有时我可以看到它,我不知道,visual 可能会在编译前尝试进行绑定,但有时会失败,我不知道不想去想:))

    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?

    推荐答案

    你的 MenuViewModel 有对应的 MenuView 吗?

    Do you have a corresponding MenuView for your MenuViewModel?

    在这种情况下,您可以像下面这样替换您的 ContentControl:

    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>
    

    通过这种方式,您可以获得 MenuView- 和 MainStuffView-usercontrols 的设计视图

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

    这篇关于在窗口上放置少量 UserControl 组件的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆