WPF MainWindow显示其他页面

时间:2017-07-06 16:19:31

标签: c# wpf xaml

我正在尝试创建一个主窗口,它将具有导航/按钮的标题和用于显示信息的页脚。 (类似于StackOverflow.com上的顶部和底部栏)

Example of how the header and footer stay in one place, while the middle section handles displaying the desired page

将主窗口作为容器显示页眉/页脚之间的任何页面的最佳方法是什么?

3 个答案:

答案 0 :(得分:4)

我会创建一个3行Grid,其中心项为ContentControl。将其绑定到视图模型上的CurrentPage属性。

创建一个Base Page类,并为要显示的每个数据页面创建一个子类。此子类也是每个页面的视图模型。

DataTemplate的每个子类添加Page以供ContentControl使用,确保指定Type属性(这是物理附加页面类型的内容)基于CurrentPage属性的UI。 DataTemplate的内容应为UserControl,其中包含每种网页类型的用户界面。

现在,当您想要更改页面时,只需将CurrentPage设置为您想要的页面实例,它就会自动更改。

此模式将使每个页面的UI与所有其他UI分开,并为它们提供强大的视图模型(Page子类)。它允许超级轻松地更改页面。并很好地遵循MVVM模式。您甚至可以将所有页面加载到您向用户显示的列表中。通过更多绑定,用户选择页面可以直接进入页面更改行为,而只需要很少的工作。

答案 1 :(得分:-2)

创建一个包含3行的页面。顶行是' auto'的高度。中间一排是高度' *'并且最下面一行是高度' auto'同样。顶部和底部行将占用页眉和页脚所需的空间,而中间行将占用房间的其余部分。

答案 2 :(得分:-2)

您可以使用网格

<Grid >
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
 <local:TopPage Grid.Column="0" Grid.Row="0" />
 <local:MidPage Grid.Column="0" Grid.Row="1" />
 <local:BottomPage Grid.Column="0" Grid.Row="2" />
 </Grid>