【发布时间】:2019-10-20 20:16:48
【问题描述】:
我有一个带有几个单选按钮、一个 ContentControl 和一个用于更改 ContentControl 内容的按钮的 MainWindow。
我还有一个带有标签的 UserControl1。当我单击 MainWindow 上的按钮将 ContentControl.Content 更改为 UserControl1 时,它会在 MainWindow 中的单选按钮顶部显示标签。我怎样才能改变它,使它像一个页面一样,并且不会将每个控件堆叠在一起?
MainWindow.xaml:
<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>
MainWindow.xaml.cs:
private void NextBtnClick(object sender, RoutedEventArgs e)
{
if (RadioBtn2.IsChecked == true)
{
this.contentControl.Content = new UserControl1();
}
}
单击下一个按钮后,我将来自 UserControl1 的控件堆叠在单选按钮的顶部。
我对 WPF 很陌生,因此我们将不胜感激任何帮助。浏览文档是一场噩梦,因为我不确定如何解决这个问题。
【问题讨论】:
-
如果要将控件堆叠在另一个下方,请使用 stackpanel 而不是 grid
-
“就像一个页面”?你真正想要什么布局?
-
@mm8 我不确定我想要什么布局。正如我所说,我是 wpf 的新手。由于它默认我使用网格布局,我使用以下答案来解决我的问题:stackoverflow.com/questions/26478742/…
-
没有“默认”。实现标记时需要选择合适的模板。
-
当我创建一个新的 WPF 项目时,在 MainWindow 的 XAML 中,它显示了 Grid。
标签: c# wpf user-controls contentcontrol