提问人:Moni 提问时间:10/29/2023 最后编辑:marc_sMoni 更新时间:10/29/2023 访问量:55
在 WPF 中从另一个视图动态更改视图
Dynamically change view from another view in WPF
问:
我正在尝试制作登录和注册页面。当我进入登录页面并单击“注册”时,运行以将内容更改为注册页面,但它没有更改。知道为什么吗?ICommand
ContentControl
MainViewModel.cs
:
using ShopWave.Core;
using ShopWave.MVVM.View;
using System;
using System.Windows.Controls;
namespace ShopWave.MVVM.ViewModel
{
internal class MainViewModel : ObservableObject
{
public RelayCommand HomeViewCommand { get; private set; }
public RelayCommand ServiceViewCommand { get; private set; }
public RelayCommand ProductViewCommand { get; private set; }
public RelayCommand SalesViewCommand { get; private set; }
public RelayCommand LoginViewCommand { get; private set; }
public RelayCommand RegisterViewCommand { get; private set; }
public RelayCommand AccountViewCommand { get; private set; }
public HomeViewModel HomeVM;
public ServiceViewModel ServiceVM;
public ProductViewModel ProductVM;
public SalesViewModel SalesVM;
public LoginViewModel LoginVM;
public RegisterViewModel RegisterVM;
public AccountViewModel AccountVM;
private ServiceView service = new ServiceView();
private object _currentView;
public object CurrentView
{
get { return _currentView; }
set
{
Console.WriteLine($"Old view: {_currentView}");
_currentView = value;
OnPropertyChanged();
Console.WriteLine($"New view: {_currentView}");
}
}
public MainViewModel()
{
HomeVM = new HomeViewModel();
ServiceVM = new ServiceViewModel();
ProductVM = new ProductViewModel();
SalesVM = new SalesViewModel();
LoginVM = new LoginViewModel();
RegisterVM = new RegisterViewModel();
AccountVM = new AccountViewModel();
CurrentView = SalesVM;
HomeViewCommand = new RelayCommand(o =>
{
CurrentView = HomeVM;
});
ServiceViewCommand = new RelayCommand(o =>
{
CurrentView = ServiceVM;
});
ProductViewCommand = new RelayCommand(o =>
{
CurrentView = ProductVM;
});
SalesViewCommand = new RelayCommand(o =>
{
CurrentView = SalesVM;
});
LoginViewCommand = new RelayCommand(o =>
{
CurrentView = LoginVM;
});
RegisterViewCommand = new RelayCommand(o =>
{
CurrentView = RegisterVM;
Console.WriteLine("RegisterViewCommand Used!");
});
AccountViewCommand = new RelayCommand(o =>
{
CurrentView = AccountVM;
});
}
}
}
LoginView.xaml
:
<UserControl x:Class="ShopWave.MVVM.View.Login"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:ShopWave.MVVM.View"
xmlns:viewModel="clr-namespace:ShopWave.MVVM.ViewModel"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<UserControl.DataContext>
<viewModel:MainViewModel/>
</UserControl.DataContext>
<Border Background="#29303d"
CornerRadius="20"
Height="400"
Width="300">
<Grid>
<Label Content="Login"
Foreground="White"
Height="47"
FontWeight="Medium"
FontSize="25"
VerticalAlignment="Top"
HorizontalAlignment="Center"
Margin="0,5,0,0" Width="80"/>
<Label Content="Email"
Foreground="White"
FontSize="22"
Margin="50,102,150,260"/>
<TextBox Width="200"
Height="30"
Background="#1f242e"
BorderThickness="0"
Margin="14,-100,0,0"
Foreground="White"
FontSize="14"
VerticalContentAlignment="Center"
HorizontalContentAlignment="Left"/>
<Label Content="Password"
Foreground="White"
FontSize="22"
Margin="50,178,110,185"/>
<PasswordBox Width="200"
Height="30"
Background="#1f242e"
BorderThickness="0"
Margin="14,50,0,0"
Foreground="White"
FontSize="14"
PasswordChar="*"
VerticalContentAlignment="Center"
HorizontalContentAlignment="Left"/>
<Border CornerRadius="10"
Width="120"
Height="50"
Background="Transparent"
Margin="0,180,0,0">
<Button Width="120"
Height="50"
BorderThickness="0"
Background="#005ce6"
Content="Login"
Foreground="White"
FontSize="25"
FontWeight="Medium">
</Button>
</Border>
<Button Background="Transparent"
Content="Register"
Foreground="#e6e6e6"
Width="60"
Height="25"
FontSize="15"
Margin="0,270,0,0"
BorderThickness="0"
VerticalContentAlignment="Center"
HorizontalContentAlignment="Center"
Click="Button2_Click" Command="{Binding RegisterViewCommand}"/>
</Grid>
</Border>
</UserControl>
MainWindow.xaml
:
<Window x:Class="ShopWave.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:viewModel="clr-namespace:ShopWave.MVVM.ViewModel"
xmlns:local="clr-namespace:ShopWave"
mc:Ignorable="d"
Height="800" Width="1120"
Loaded="WindowLoaded"
WindowStyle="None"
AllowsTransparency="True"
Background="Transparent"
>
<Window.DataContext>
<viewModel:MainViewModel/>
</Window.DataContext>
<Border Background="#1f242e"
CornerRadius="20">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="168" />
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="62" />
<RowDefinition Height="489*"/>
</Grid.RowDefinitions>
<TextBlock Text="Shop Wave"
VerticalAlignment="Center"
HorizontalAlignment="Left"
Foreground="White"
FontSize="22"
Margin="20,0,0,0" Height="30" Width="109"/>
<StackPanel Grid.Row="1">
<RadioButton Content="Services"
Height="50"
Foreground="White"
FontSize="18"
Style="{StaticResource MenuButtonTheme}"
Command="{Binding ServiceViewCommand}"/>
<RadioButton Content="Products"
Height="50"
Foreground="White"
FontSize="18"
Style="{StaticResource MenuButtonTheme}"
Command="{Binding ProductViewCommand}"/>
<RadioButton Content="Sales"
Height="50"
Foreground="White"
FontSize="18"
IsChecked="True"
Style="{StaticResource MenuButtonTheme}"
Command="{Binding SalesViewCommand}"/>
<RadioButton Content="Account"
Height="50"
Foreground="White"
FontSize="18"
Margin="0, 520"
Style="{StaticResource MenuButtonTheme}"
Command="{Binding LoginViewCommand}"/>
</StackPanel>
<TextBox Width="250"
VerticalContentAlignment="Center"
HorizontalAlignment="Left"
Margin="5,11,0,11"
Grid.Column="1"
Style="{StaticResource ModernTextBox}"/>
<ContentControl Grid.Row="1"
Grid.Column="1"
Margin="10"
Content="{Binding CurrentView}">
</ContentControl>
</Grid>
</Border>
</Window>
RegisterView.xaml
:
<UserControl x:Class="ShopWave.MVVM.View.RegisterView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:ShopWave.MVVM.View"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<Border Background="#29303d"
CornerRadius="20"
Height="400"
Width="300">
<Grid>
<Label Content="Register"
Foreground="White"
Height="47"
FontWeight="Medium"
FontSize="25"
VerticalAlignment="Top"
HorizontalAlignment="Center"
Margin="0,5,0,0"
Width="105"
/>
<RadioButton Content="Business"
Foreground="White"
FontSize="18"
VerticalAlignment="Center"
VerticalContentAlignment="Center"
HorizontalAlignment="Center"
HorizontalContentAlignment="Center"
Margin="0,-240,0,0"
x:Name="business"/>
<Label Content="Username"
Foreground="White"
FontSize="22"
Margin="50,102,143,260"/>
<TextBox Width="200"
Height="30"
Background="#1f242e"
BorderThickness="0"
Margin="14,-100,0,0"
Foreground="White"
FontSize="14"
VerticalContentAlignment="Center"
HorizontalContentAlignment="Left"
x:Name="username"/>
<Label Content="Email"
Foreground="White"
FontSize="22"
Margin="50,162,150,196"/>
<TextBox Width="200"
Height="30"
Background="#1f242e"
BorderThickness="0"
Margin="14,20,0,0"
Foreground="White"
FontSize="14"
VerticalContentAlignment="Center"
HorizontalContentAlignment="Left"
x:Name="email"/>
<Label Content="Password"
Foreground="White"
FontSize="22"
Margin="50,220,110,139"/>
<PasswordBox Width="200"
Height="30"
Background="#1f242e"
BorderThickness="0"
Margin="14,140,0,0"
Foreground="White"
FontSize="14"
PasswordChar="*"
VerticalContentAlignment="Center"
HorizontalContentAlignment="Left"
x:Name="password"/>
<Border CornerRadius="10"
Width="120"
Height="50"
Background="Transparent"
Margin="0,240,0,0">
<Button Width="120"
Height="50"
BorderThickness="0"
Background="#005ce6"
Content="Register"
Foreground="White"
FontSize="25"
FontWeight="Medium"
Click="Button_Click">
</Button>
</Border>
<Button Background="Transparent"
IsHitTestVisible="False"
Content="Login"
Foreground="#e6e6e6"
Width="60"
Height="25"
FontSize="15"
Margin="0,330,0,0"
BorderThickness="0"
VerticalContentAlignment="Center"
HorizontalContentAlignment="Center"/>
</Grid>
</Border>
</UserControl>
有谁知道为什么会这样?任何帮助都是值得赞赏的!
答: 暂无答案
评论
CurrentView
是一个对象,则您分配的是 ViewModel 实例,而不是视图。FWIW,查看数据模板并根据数据类型弹出视图,即:视图模型。