wpf 调用 page,显示页面
时间: 2024-11-03 07:19:23 浏览: 153
在Windows Presentation Foundation (WPF)中,要动态加载并显示一个Page,通常你会使用`Frame`控件或者是`DataTemplate`配合`ContentControl`。以下是两种常见的方式:
1. **使用`Frame`**:
- 在你的窗口或其他容器上添加一个`Frame`控件:
```xml
<Window x:Class="YourNamespace.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid>
<Frame Name="pageFrame" />
</Grid>
</Window>
```
- 在运行时,通过代码注入新页面:
```csharp
Page yourNewPage = new YourNewPage();
frame.Navigate(yourNewPage);
```
2. **使用`DataTemplate`和`ContentControl`**:
- 定义一个`DataTemplate`,指定页面的类型:
```xaml
<DataTemplate DataType="{x:Type local:YourNewPage}">
<local:YourNewPage/>
</DataTemplate>
```
- 在需要的地方使用`ContentControl`并设置其模板:
```xaml
<ContentControl ContentTemplate="{StaticResource YourTemplateName}" />
```
- 在运行时,替换`ContentControl`的内容:
```csharp
contentControl.Content = new YourNewPage();
```
阅读全文
相关推荐

















