WPF:动态视图/内容 [英] WPF : dynamic view/content

查看:24
本文介绍了WPF:动态视图/内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 WPF 有点初学者,所以我问这个..

I'm a bit beginner in WPF, so I ask this..

假设我有一个窗口,在窗口内我想要一些类似容器的东西,可能只是边框或面板(在 winform 术语中).容器的内容绑定到选定的选项(例如:按钮).因此,例如,当用户选择 OPTION 1 时,容器显示图表;当用户选择选项 2 时,容器显示填充数据的列表视图;当用户选择 OPTION 3 时,容器会显示其他内容,依此类推.

Let's say I have a window, and inside the window I want to have something like container, could be just border or maybe panel (in winform terms). The content of container is binded to the selected option (e.g:button). So, for instance, when user selects OPTION 1, the container shows chart; when user selects OPTION 2, the container shows listview filled with data; when user selects OPTION 3, the container shows another things, and so on.

执行此操作的最佳/最好(或最简单)方法是什么?我正在考虑对容器的内容使用用户控件,但不知道这是否是一个不错的解决方案,也不是使用用户控件来显示一些复杂的东西的性能,也许还有一些计算.大家还有别的想法吗?

What is the best/nicest (or easiest maybe) approach to do this? I'm thinking about using user control for the content of the container, but don't know if this is nice solution neither the performance for using user control to show little bit complex things and maybe some calculations. Any other idea guys?

推荐答案

我用 ContentControl

主窗口:(定义您希望可视化为资源的视图)

MainWindow: (Define the views you wish to visualize as resources)

<Window.Resources>
    <DataTemplate DataType="{x:Type viewModels:SystemPCViewModel}">
        <controls:SystemPCControl/>
    </DataTemplate>
    <DataTemplate DataType="{x:Type viewModels:ChipPCViewModel}">
        <controls:ChipPCControl/>
    </DataTemplate>
    </ResourceDictionary>

</Window.Resources>

<Grid>
   <ContentControl Content="{Binding CurrentView}"/>
</Grid>

ViewModel:(再简单不过了)

ViewModel: (can't get much simpler)

public ViewModelBase CurrentView
{
    get { return currentView; }
    set { Set(() => CurrentView, ref currentView, value); }
}

然后,您可以通过为您在 MainWindow

And there you go, you can change your views by setting the view model for the controls you defined in your MainWindow

private void OnCommandExecuted()
{
    CurrentView = someViewModel;
}

private void OnAnotherCommandExecuted()
{
    CurrentView = anotherViewModel;
}

HTH!

这篇关于WPF:动态视图/内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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