如何在WPF中的窗口之间导航? (不是浏览器)

时间:2019-04-08 01:32:18

标签: c# wpf navigation

我想开发一个基于WPF的桌面应用程序。如何在一个窗口中浏览C#代码到一个窗口中的另一个窗口?换句话说,我试图为单选按钮设置一个click事件,该事件将在框架或边框中打开另一个窗口。

<Window x:Class="WpfNavigation2.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:WpfNavigation2"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded" Closing="Window_Closing">
    <Grid>
        <Border Margin="10,100,10,10" Background="AliceBlue">
            <Grid Name="contergrid">

            </Grid>
        </Border>


        <RadioButton x:Name="radioButton" Content="RadioButton1" HorizontalAlignment="Left" Margin="60,0,0,0" VerticalAlignment="Top" Click="btn1"/>
        <RadioButton x:Name="radioButton1" Content="RadioButton2" HorizontalAlignment="Left" Margin="60,37,0,0" VerticalAlignment="Top" Click="btn2"/>
        <RadioButton x:Name="radioButton2" Content="RadioButton3" HorizontalAlignment="Left" Margin="60,68,0,0" VerticalAlignment="Top" Click="btn3"/>

    </Grid>
</Window>


        private void btn1(object sender, RoutedEventArgs e)
        {
           //open window 1 in border
         }

1 个答案:

答案 0 :(得分:0)

您正在寻找的是ContentPresenter。您可以将各种内容放入ContentPresenter中,例如控件

    <Border Margin="10,100,10,10" Background="AliceBlue">
        <ContentPresenter Name="contentFrame">

        </ContentPresenter>
    </Border>


    private void btn1(object sender, RoutedEventArgs e)
    {
        Grid grd = new Grid();
        grd.Background = new SolidColorBrush(Colors.HotPink);

        contentFrame.Content = grd; // replace me with your control / page / window1
    }