[英]How to create a base page that includes a common header for all pages extending that?
I'm very new to WPF application development. 我是WPF应用程序开发的新手。
I'm writing a simple WPF application in which I'll just have one window and pages will change on click buttons provided in UI. 我正在编写一个简单的WPF应用程序,其中只有一个窗口,并且UI中提供的单击按钮将更改页面。
Here how can have base page in which i'll add some header which is common for all pages i'm designing. 在这里,如何拥有一个基本页面,在其中添加一些我正在设计的所有页面通用的标题。
Is there anything like by extending that BasePage, all controls will come to this page? 是否有像通过扩展BasePage一样的方式,所有控件都将到达此页面?
For example, My app title should come in all page which I don't want to add in XAMLs of all pages. 例如,我的应用程序标题应该出现在我不想在所有页面的XAML中添加的所有页面中。
Please let me know if my assumption itself is wrong. 如果我的假设本身是错误的,请告诉我。
Edited: 编辑:
My BasePage: 我的基本页面:
public class BasePage : Page
{
public BasePage()
{
}
}
XAML of my HomePage: 我的主页的XAML:
<local:BasePage x:Class="CCS.ui.HomePage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:My.ui"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300"
Title="HomePage">
<StackPanel>
<WrapPanel Name="root"></WrapPanel>
<Button Name="Home" Content="Home"></Button>
<Button Name="Home3" Content="Home2"></Button>
</StackPanel>
Here I'm not sure where to write controls of header which should be added to BasePage. 在这里,我不确定应该在何处编写应添加到BasePage的标头控件。 Because if BasePage is created from XAML It is showing error like "Cannot use Pages generated from XAML". 因为如果BasePage是从XAML创建的,它会显示类似“无法使用从XAML生成的页面”之类的错误。
There are several ways to do what you want. 有几种方法可以做您想要的。 Here I put a small example: I created a Window with 3 main controls, Menu (for navigation), TextBox (Header) and a Grid (Host) When I click on the menu, I add a UserControl in the HostGrid. 在这里,我举一个小例子:我创建了一个带有3个主要控件的窗口,分别是Menu(用于导航),TextBox(页眉)和Grid(主机)。单击菜单时,我在HostGrid中添加了一个UserControl。
MainWindow.xaml MainWindow.xaml
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
<RowDefinition/>
</Grid.RowDefinitions>
<Menu>
<MenuItem Header="Home" x:Name="Home" Click="MenuItem_Click"/>
<MenuItem Header="Page 1" x:Name="Page1" Click="MenuItem_Click"/>
<MenuItem Header="Page 2" x:Name="Page2" Click="MenuItem_Click"/>
</Menu>
<TextBlock Grid.Row="1" Text=" Header" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="24" Height="40"/>
<Grid Grid.Row="2" x:Name="HostGrid"></Grid>
</Grid>
MainWindow.cs MainWindow.cs
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void MenuItem_Click(object sender, RoutedEventArgs e)
{
HostGrid.Children.Clear();
switch(((MenuItem)e.OriginalSource).Name)
{
case "Home":
HostGrid.Children.Add(new UserControlHome());
break;
case "Page1":
HostGrid.Children.Add(new UserControl1());
break;
case "Page2":
HostGrid.Children.Add(new UserControl2());
break;
}
}
}
UserControlHome.xaml UserControlHome.xaml
<Grid Background="Crimson">
<TextBlock Text="Home" FontSize="18" VerticalAlignment="Center" HorizontalAlignment="Center"/>
</Grid>
UserControl1.xaml UserControl1.xaml
<Grid Background="Lavender">
<TextBlock Text="Page 1" FontSize="18" VerticalAlignment="Center" HorizontalAlignment="Center"/>
</Grid>
UserControl2.xaml UserControl2.xaml
<Grid Background="Ivory">
<TextBlock Text="Page 2" FontSize="18" VerticalAlignment="Center" HorizontalAlignment="Center"/>
</Grid>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.