简体   繁体   English

窗口C#/ WPF中的动态内容

[英]Dynamic content in a window C#/WPF

I need to make an app which will have only a window, and you will change the window's content on clicking on a button. 我需要制作一个只有一个窗口的应用程序,然后单击按钮即可更改窗口的内容。

I don't know how to do that. 我不知道该怎么做。 I think of use User Controls, but I don't think it's the best way. 我考虑使用用户控件,但我认为这不是最好的方法。 Perhaps with tab, but it won't respect the UI I want to have. 也许带有选项卡,但它不会尊重我想要的UI。 So how is the best way to archieve that ? 那么最好的存档方式是什么? Dynamic content on a WPF window (of course, I will need to pass data and variables through the differents view) WPF窗口上的动态内容(当然,我将需要通过差异视图传递数据和变量)

Here's a "example" of the UI I want to do. 这是我要执行的UI的“示例”。 Excuse if it's not good, I do that as fast as possible :) 请问,如果不好的话,我会尽快完成:)

UI示例

MVVM wise you will need different view for each page (and some base class for each view). MVVM明智的做法是,每个页面都需要不同的视图(每个视图都需要一些基类)。 So UserControl is the way to go. 因此, UserControl是必经之路。

However, it is possible to use TabControl for multiple pages with collapsed headers: 但是,可以对带有折叠标题的多个页面使用TabControl

    <TabControl>
        <TabItem Visibility="Collapsed">
            <Label Content="First page"/>
        </TabItem>
        <TabItem Visibility="Collapsed">
            <Label Content="Second page"/>
        </TabItem>
    </TabControl>

To edit specific page you select corresponding item in designer and then you can see its content. 要编辑特定页面,请在设计器中选择相应的项目,然后可以查看其内容。

For buttons you will need a special area (because buttons are common between pages). 对于按钮,您将需要一个特殊的区域(因为按钮在页面之间是通用的)。 So that your window will contain grid like 这样您的窗口将包含像

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>
    <Image Grid.Row="0" Stretch="None" HorizontalAlignment="Left" VerticalAlignment="Top" Source="logo.png"/>
    <TabControl Grid.Row="1" SelectedIndex="{Binding Page}">
        ... tab items
    </TabControl>
    <StackPanel Grid.Row="2" Orientation="Horizontal" FlowDirection="RightToLeft">
        <Button Command="{Binding CommandFinish}" Content="Finish"/>
        <Button Command="{Binding CommandNext}" Content="Next"/>
        <Button Command="{Binding CommandPrevious}" Content="Previous"/>
        ... and so on, from right to left
    </StackPanel>
</Grid>

And to utilize MVVM at least a bit, you could have page numbers, so that your ViewModel knows about how many pages have to be total and what each page can do (to control button visibility and navigation). 要至少利用MVVM,您可以有页码,这样您的ViewModel可以知道总共必须有多少页,以及每页可以做什么(以控制按钮的可见性和导航)。

The image you provide looks like a typical wizard. 您提供的图像看起来像一个典型的向导。 The Extended WPF Toolkit already ships with a Wizard. Extended WPF Toolkit已随附向导。 And it's completely free. 而且它是完全免费的。 I'd recommend you to at least take a look at it. 我建议您至少看看它。 It allows you to apply any kind of content to any page. 它允许您将任何种类的内容应用于任何页面。 Also the behavior of the wizard itself can be modified. 向导本身的行为也可以修改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM