C# 更改ContentControl。内容将内容堆叠在彼此的顶部
我有一个带有两个单选按钮的主窗口,一个ContentControl和一个更改ContentControl内容的按钮 我还有一个带有标签的UserControl1。当我单击MainWindow上的按钮将ContentControl.Content更改为UserControl1时,它会在我从MainWindow获得的单选按钮顶部显示标签。我怎样才能改变它,使它像一个页面一样工作,而不是将每个控件堆叠在另一个控件之上 MainWindow.xaml:C# 更改ContentControl。内容将内容堆叠在彼此的顶部,c#,wpf,user-controls,contentcontrol,C#,Wpf,User Controls,Contentcontrol,我有一个带有两个单选按钮的主窗口,一个ContentControl和一个更改ContentControl内容的按钮 我还有一个带有标签的UserControl1。当我单击MainWindow上的按钮将ContentControl.Content更改为UserControl1时,它会在我从MainWindow获得的单选按钮顶部显示标签。我怎样才能改变它,使它像一个页面一样工作,而不是将每个控件堆叠在另一个控件之上 MainWindow.xaml: <Window x:Class="Test.
<Window x:Class="Test.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:Test"
mc:Ignorable="d"
Title="Test" WindowStartupLocation="CenterScreen" Width="968" Height="560" HorizontalAlignment="Center" VerticalAlignment="Center">
<Grid HorizontalAlignment="Center" VerticalAlignment="Center" Height="540" Margin="0,10,-6,-19" Width="968">
<StackPanel Height="74" Margin="731,446,0,0" VerticalAlignment="Top" Width="229" Orientation="Horizontal" HorizontalAlignment="Left">
<Button Content="Back" MinWidth="100" Margin="10,20,0,0"/>
<Button Content="Next" MinWidth="100" Margin="10,20,0,0" Click="NextBtnClick"/>
</StackPanel>
<RadioButton x:Name="RadioBtn1" Content="Radio1" HorizontalAlignment="Center" Margin="312,130,95,0" VerticalAlignment="Top" Height="76" Width="561" FontSize="48" FontWeight="Bold" GroupName="1"/>
<RadioButton x:Name="RadioBtn2" Content="Radio2" HorizontalAlignment="Center" Margin="312,232,95,0" VerticalAlignment="Top" Height="76" Width="561" FontSize="48" FontWeight="Bold" GroupName="1"/>
<ContentControl x:Name="contentControl" Grid.Row="1"/>
</Grid>
</Window>
单击“下一步”按钮后,我从UserControl1获得了堆叠在单选按钮顶部的控件
我是WPF的新手,因此非常感谢您的帮助。浏览文档是一场噩梦,因为我不知道如何解决这个问题。您可以尝试使用
网格
面板,而无需定义行和列定义。根据您的代码,您正在尝试通过硬编码的边距、宽度和高度来定义面板布局。这就是为什么控件彼此重叠渲染的原因
我修改了网格代码来定义行定义,以便控件按行堆叠。因此,当您单击按钮时,contentcontroll
被加载到最后一行So(通过RowDefinition定义为Height=“*”,以占用剩余空间)
你们可以在互联网上阅读WPF面板和网格布局。这可能是一个好的开始
<Window x:Class="Test.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:Test"
mc:Ignorable="d"
Title="Test" WindowStartupLocation="CenterScreen" Width="968" Height="560" HorizontalAlignment="Center" VerticalAlignment="Center">
<Grid HorizontalAlignment="Center" VerticalAlignment="Center" Margin="10 10 10 10">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions?
<StackPanel Grid.Row="0" Orientation="Horizontal" HorizontalAlignment="Left">
<Button Content="Back" MinWidth="100" />
<Button Content="Next" MinWidth="100" Click="NextBtnClick"/>
</StackPanel>
<RadioButton Grid.Row="1" x:Name="RadioBtn1" Content="Radio1" HorizontalAlignment="Center" VerticalAlignment="Top" FontWeight="Bold" GroupName="1"/>
<RadioButton Grid.Row="2" x:Name="RadioBtn2" Content="Radio2" HorizontalAlignment="Center" FontSize="48" FontWeight="Bold" GroupName="1"/>
<ContentControl x:Name="contentControl" Grid.Row="3"/>
</Grid>
</Window>
使用网格有点烦人。我只希望能够单击按钮并更改窗口上的所有内容,以切换到不同的UserControl。如果我找不到更简单的解决方案,我可能不得不用这个。谢谢发布的代码也需要适当的布局。如果要将控件一个一个堆叠在另一个之下,请使用stackpanel而不是grid“像页面一样的行为”?你到底想要什么布局?@mm8我不知道我想要什么布局。正如我所说,我是wpf的新手。由于它将我默认为网格布局,所以我使用以下答案来解决我的问题:没有“默认”。在实现标记时,需要选择适当的模板。当我在XAML for Main窗口中创建新的WPF项目时,它会显示网格。
<Window x:Class="Test.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:Test"
mc:Ignorable="d"
Title="Test" WindowStartupLocation="CenterScreen" Width="968" Height="560" HorizontalAlignment="Center" VerticalAlignment="Center">
<Grid HorizontalAlignment="Center" VerticalAlignment="Center" Margin="10 10 10 10">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions?
<StackPanel Grid.Row="0" Orientation="Horizontal" HorizontalAlignment="Left">
<Button Content="Back" MinWidth="100" />
<Button Content="Next" MinWidth="100" Click="NextBtnClick"/>
</StackPanel>
<RadioButton Grid.Row="1" x:Name="RadioBtn1" Content="Radio1" HorizontalAlignment="Center" VerticalAlignment="Top" FontWeight="Bold" GroupName="1"/>
<RadioButton Grid.Row="2" x:Name="RadioBtn2" Content="Radio2" HorizontalAlignment="Center" FontSize="48" FontWeight="Bold" GroupName="1"/>
<ContentControl x:Name="contentControl" Grid.Row="3"/>
</Grid>
</Window>