我正在创建用于教育目的的应用程序。如何在“ MainWindow.xaml”中使用多个用户控件?
我想在MainWindow上使用User控件,这样我就不需要多个窗口。因此,在注册布局上按下一步后,
它应该带您到感谢屏幕,它也是另一个UserControl类。虽然在同一窗口中。
我已经阅读了尽可能多的不同“解决方案”,没有任何运气。
这是我有atm的代码。
Main Window.xaml
db.json
MainWindow.xaml.cs 没有代码...
SignUpControl.Xaml
<Window x:Class="WpfApp1.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:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="700" Width="700"
WindowStartupLocation="CenterScreen">
<Grid>
<!--Background image-->
<Grid.Background >
<ImageBrush ImageSource="login-page-background-3.jpg"/>
</Grid.Background>
<!--Main content scroll-->
<ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto">
<local:SignUpControl>
</local:SignUpControl>
</ScrollViewer>
</Grid>
我该如何解决这个问题,我可以在同一窗口的用户控件之间进行切换,因此,在谢谢屏幕之后,我将使用相同的逻辑转到“登录!”。等等...
答案 0 :(得分:1)
当我不使用Prism
或Dependency Injection
时,我通常会先使用视图模型。
在这种情况下,我们在Windows ViewModel上有一个属性,该属性是其他UserControls
ViewModels继承的类,通常只使用隐含INotifyPropertyChanged
的ViewModelBase类:
private ViewModelBase currentViewModel;
public ViewModelBase CurrentViewModel
{
get { return currentViewModel; }
set { currentViewModel = value; NotifyPropertyChanged(); }
}
现在在{@ {Tomtom}之类的MainWindow
内,您已经绑定到该属性ContentControl
。这意味着使用DataTemplates
可以具有不同的View显示,具体取决于该属性上当前使用的ViewModel类型。
<Window.Resources>
<DataTemplate DataType="{x:Type viewmodels:ViewModel1}">
<views:View1/>
</DataTemplate>
<DataTemplate DataType="{x:Type viewmodels:ViewModel2}">
<views:View2/>
</DataTemplate>
</Window.Resources>
<ContentControl Content="{Binding CurrentViewModel}"/>
完成此操作后,您需要做的就是更改MainWindow
ViewModel上的ViewModel的逻辑,然后视图将更新以显示该ViewModel的正确视图。
编辑:要注意的一件事是,人们使用了许多不同的方法来实现自己想要的东西,它们都不是对与错,但是它们都适合不同的人们的需求和编码风格。
答案 1 :(得分:0)