0
  • 在我的 c# Wpf 独立应用程序中,我创建了主窗口并在我的项目中添加了另外 5 个窗口
  • 我在主窗口中按这 5 个窗口中每个窗口的名称放置了一个按钮。
  • 我的问题是:当我按下主窗口中的任何按钮时..如何在主窗口的(Windows 区域)中显示其相关窗口?例如,如果我按下历史按钮..如何在主窗口的那个(Windows 区域)中显示历史窗口?
  • 注意:我不想在我的应用程序中使用带有导航控件的页面或导航窗口。

在此处输入图像描述

4

1 回答 1

3

当您单击按钮时,只需使用 UserControl 并将它们添加到您的容器控件中。

这是一个简单的例子:

主窗口.xaml

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Button Content="History" Height="53" HorizontalAlignment="Left" Margin="12,12,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
        <Button Content="Precaution" Height="53" HorizontalAlignment="Left" Margin="109,12,0,0" Name="button2" VerticalAlignment="Top" Width="75" Click="button2_Click" />
        <Button Content="Uses" Height="53" HorizontalAlignment="Left" Margin="208,12,0,0" Name="button3" VerticalAlignment="Top" Width="75" Click="button3_Click" />
        <Button Content="Side Effects" Height="53" HorizontalAlignment="Left" Margin="314,12,0,0" Name="button4" VerticalAlignment="Top" Width="75" Click="button4_Click" />
        <Button Content="New Item" Height="53" HorizontalAlignment="Left" Margin="405,12,0,0" Name="button5" VerticalAlignment="Top" Width="75" Click="button5_Click" />
        <Viewbox Height="209" HorizontalAlignment="Left" Margin="12,90,0,0" Name="Container" VerticalAlignment="Top" Width="479" />
    </Grid>
</Window>

主窗口.xaml.cs

public partial class MainWindow : Window
{
    History use1 = new History();
    Precaution use2 = new Precaution();
    Uses use3 = new Uses();
    SideEffect use4 = new SideEffect();
    NewItem use5 = new NewItem();

    public MainWindow()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        Container.Child = use1;
    }

    private void button2_Click(object sender, RoutedEventArgs e)
    {
        Container.Child = use2;
    }

    private void button3_Click(object sender, RoutedEventArgs e)
    {
        Container.Child = use3;
    }

    private void button4_Click(object sender, RoutedEventArgs e)
    {
        Container.Child = use4;
    }

    private void button5_Click(object sender, RoutedEventArgs e)
    {
        Container.Child = use5;
    }
}

虚拟用户控件

<UserControl x:Class="WpfApplication1.History"
         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" 
         mc:Ignorable="d" 
         d:DesignHeight="209" d:DesignWidth="479" Background="AliceBlue">
    <Grid Width="479" Height="209">

    </Grid>
</UserControl>

<UserControl x:Class="WpfApplication1.Precaution"
         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" 
         mc:Ignorable="d" 
         d:DesignHeight="209" d:DesignWidth="479" Background="LightCoral" >
    <Grid Width="479" Height="209">

    </Grid>
</UserControl>

<UserControl x:Class="WpfApplication1.Uses"
         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" 
         mc:Ignorable="d" 
         d:DesignHeight="209" d:DesignWidth="479" Background="LightGreen" >
    <Grid Width="479" Height="209">

    </Grid>
</UserControl>

<UserControl x:Class="WpfApplication1.SideEffect"
         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" 
         mc:Ignorable="d" 
         d:DesignHeight="209" d:DesignWidth="479" Background="PapayaWhip" >
    <Grid Height="209" Width="479">

    </Grid>
</UserControl>

<UserControl x:Class="WpfApplication1.NewItem"
         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" 
         mc:Ignorable="d" 
         d:DesignHeight="209" d:DesignWidth="479" Background="LightGoldenrodYellow">
    <Grid Height="209" Width="479">

    </Grid>
</UserControl>
于 2012-12-27T03:26:23.217 回答