我想开发一个基于 WPF 的桌面应用程序。如何在窗口中从一个窗口导航到另一个窗口的 C# 代码?换句话说,我尝试为在框架或边框中打开另一个窗口的单选按钮设置单击事件。
<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
}
您正在寻找的是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
}