简体   繁体   中英

How to navigate between windows in WPF? (not browser)

I want to develop a desktop application based on WPF. How do I navigate through C # code from one window to another in a window? in the other word, I have tried to set up a click event for a radiobutton that opens another window in frame or border.

<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
         }

What you are looking for is a ContentPresenter. You can put all kinds of content in a ContentPresenter for example a control

    <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
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM