WPF母版页问题 [英] WPF Master Page Issue
问题描述
我有一个Windows 7旗舰版32位系统。我正在使用Visual Studio 2013 Ultimate。是否有任何概念 " Master
Page" 在WPF应用程序中我可以在ASP.NET Web表单应用程序中使用吗?如果有,我将如何配置
呢?请帮助。
推荐答案
主页有效地围绕页面。
A master page effectively just surrounds the pages.
一种简单的方法做类似的事情是一个单一的窗口应用程序。
A simple way to do something similar is a single window app.
顺便说一下,这是一个非常常见的模式。
This is quite a common pattern, by the way.
要导航你切换出用户控件进入内容控制。
To navigate you switch out usercontrols which go in a contentcontrol.
这是一个给你一个味道的基本例子:
Here's a basic example to give you a flavour:
<Window x:Class="wpf_Navigation_Basic.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:wpf_Navigation_Basic"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="120"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<ListBox HorizontalContentAlignment="Stretch"
Button.Click="Button_Click"
>
<Button Content="Home" Tag="{x:Type local:HomeView}"/>
<Button Content="Departments" Tag="{x:Type local:DepartmentView}"/>
</ListBox>
<ContentControl Name="MyContentControl"
Grid.Column="1"/>
</Grid>
</Grid>
</Window>
查看MyContentControl?
See that MyContentControl?
托管用户控件。
将它们视为页面。
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Navigate(typeof(HomeView));
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Button btn = (Button)e.OriginalSource;
Navigate(btn.Tag as Type);
}
private void Navigate (Type viewType)
{
UserControl uc;
if (Views.ContainsKey(viewType))
{
uc = Views[viewType];
}
else
{
uc = (UserControl)Activator.CreateInstance(viewType);
Views[viewType] = uc;
}
MyContentControl.Content = uc;
}
private Dictionary<Type, UserControl> Views = new Dictionary<Type, UserControl>();
}
这是一个基本的介绍性片段。
This is a basic introductory sort of a snippet.
对于更复杂的方法,您可能需要标准化菜单。
For a more sophisticated approach you might want a standardised menu.
您还想使用MVVM,这是wpf开发的事实标准模式。
You'd also want to use MVVM, which is the de facto standard pattern for wpf development.
含义contentcontrol"导航" ;将是viewmodel首先。绑定内容,将其设置为视图模型,该视图模型被模板化为viewmodel作为datacontext的视图。
Meaning the contentcontrol "Navigation" would be viewmodel first. Bind the content, set that to a viewmodel which is templated into a view with the viewmodel as datacontext.
然后菜单中的按钮将绑定到视图模型公开的任何命令。
Your buttons in the menu would then bind to whatever commands the viewmodel exposed.
但是在一个帖子中解释的很多。
But that's quite a lot to explain in one thread.
假设你是新手。
这篇关于WPF母版页问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!