嗨,我最近两天一直在找,但是我没有办法解决问题。
这是我的mainView
MainView.xaml
<Window x:Class="STARename.MainView"
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:vm="clr-namespace:PL.STARename.ViewModels"
xmlns:views="clr-namespace:PL.STARename.Views"
xmlns:local="clr-namespace:STARename"
mc:Ignorable="d"
Title="{Binding Path=Title}" Height="650" Width="825" WindowStartupLocation="CenterScreen">
<Window.DataContext>
<vm:MainViewModel />
</Window.DataContext>
<Window.Resources>
<DataTemplate DataType="{x:Type vm:LoginViewModel}">
<views:LoginView />
</DataTemplate>
<DataTemplate DataType="{x:Type vm:STARViewModel}">
<views:STARView />
</DataTemplate>
</Window.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="109*"/>
<ColumnDefinition Width="166*"/>
<ColumnDefinition Width="228*"/>
<ColumnDefinition Width="213*"/>
<ColumnDefinition Width="101*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="56*"/>
<RowDefinition Height="484*"/>
<RowDefinition Height="79*"/>
</Grid.RowDefinitions>
<Image Source="{Binding ContiLogo}" Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="2" />
<TextBlock Text="{Binding Path=Title}" Grid.Column="2" FontWeight="Bold" FontFamily="Engravers MT" FontSize="24" VerticalAlignment="Center" HorizontalAlignment="Center" />
<ContentControl Content="{Binding CurrentViewModel}" Grid.Column="1" Grid.Row="1" Grid.ColumnSpan="3" />
</Grid>
</Window>
LoginView.xaml
<UserControl x:Class="PL.STARename.Views.LoginView"
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:vm="clr-namespace:PL.STARename.ViewModels"
xmlns:local="clr-namespace:PL.STARename.Views"
mc:Ignorable="d"
Height="567.771" Width="518.51">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="80*"/>
<ColumnDefinition Width="344*"/>
<ColumnDefinition Width="87*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="98*"/>
<RowDefinition Height="230*"/>
<RowDefinition Height="209*"/>
</Grid.RowDefinitions>
<GroupBox x:Name="groupBox" Header="Login" Grid.Column="1" Grid.Row="1" BorderBrush="Orange">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="18*"/>
<ColumnDefinition Width="92*"/>
<ColumnDefinition Width="23*"/>
<ColumnDefinition Width="70*"/>
<ColumnDefinition Width="109*"/>
<ColumnDefinition Width="20*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="81*"/>
<RowDefinition Height="35*"/>
<RowDefinition Height="12*"/>
<RowDefinition Height="32*"/>
<RowDefinition Height="44*"/>
<RowDefinition Height="4*"/>
</Grid.RowDefinitions>
<Image Source="{Binding ContiLogo}" Grid.ColumnSpan="4" />
<TextBlock Text="User:" Grid.Column="1" Grid.Row="1" />
<TextBlock Text="Password:" Grid.Column="1" Grid.Row="3" />
<TextBox Text="{Binding Path=Username, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Grid.Column="3" Grid.Row="1" Grid.ColumnSpan="2" />
<PasswordBox Grid.Column="3" Grid.Row="3" x:Name="PasswordBox" vm:PassAssist.BindPassword="True" vm:PassAssist.BoundPassword="{Binding Path=Password, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Grid.ColumnSpan="2">
<PasswordBox.InputBindings>
<KeyBinding Key="Enter" Command="{Binding CmdLogin}" />
</PasswordBox.InputBindings>
</PasswordBox>
<Button Background="Orange" Content="Login" Grid.Column="1" Grid.Row="4" Command="{Binding CmdLogin}" />
<TextBlock Text="{Binding Path=LoginText,Mode=TwoWay}" Grid.Column="3" Grid.ColumnSpan="2" Grid.Row="4" />
</Grid>
</GroupBox>
</Grid>
</UserControl>
STARView.xaml
<UserControl x:Class="PL.STARename.Views.STARView"
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:PL.STARename.Views"
xmlns:vm="clr-namespace:PL.STARename.ViewModels"
mc:Ignorable="d"
Height="567.771" Width="518.51">
<Grid>
<TextBlock Text="STAR" />
</Grid>
</UserControl>
BaseViewModel.cs
#region Variables
public string Title
{
get
{
return $"{Settings.Default.AppName} {Settings.Default.AppVersion}";
}
}
public string ContiLogo
{
get
{
return $@"{Environment.CurrentDirectory}/Others/ContiLogo.png";
}
}
protected virtual void SetProperty<T>(ref T member, T val,
[CallerMemberName] string propertyName = null)
{
if (object.Equals(member, val)) return;
member = val;
OnPropertyChanged(propertyName);
}
public event PropertyChangedEventHandler PropertyChanged;
#endregion
protected void OnPropertyChanged(string propertyName)
{
VerifyPropertyName(propertyName);
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
[Conditional("DEBUG")]
private void VerifyPropertyName(string propertyName)
{
if (TypeDescriptor.GetProperties(this)[propertyName] == null)
throw new ArgumentNullException(GetType().Name + " does not contain property: " + propertyName);
}
public BaseViewModel()
{
//Helpers.BusLayer._BL;
try
{
Helpers.BusLayer._BL.InitorReset();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
MainViewModel.cs
public class MainViewModel : BaseViewModel
{
#region Variables
public LoginViewModel LoginVM;
public STARViewModel STARVM;
private BaseViewModel _currentVM;
public BaseViewModel CurrentViewModel
{
get
{
return _currentVM;
}
set
{
if (_currentVM == value)
return;
_currentVM = value;
OnPropertyChanged("CurrentViewModel");
}
}
#endregion
public MainViewModel()
{
LoginVM = new LoginViewModel();
STARVM = new STARViewModel();
if (!Helpers.BusLayer.LoginSuccess)
CurrentViewModel = LoginVM;
else
CurrentViewModel = STARVM;
}
}
当我的登录名为true时,请输入LoginViewmodel,我想将View更改为STARView / ViewModel,但是View并没有更改。
LoginViewModel.cs
#region Variables
private ICommand _cmdLogin;
public ICommand CmdLogin
{
get
{
return _cmdLogin ?? (_cmdLogin = new CommandHandler(Login, canLogin));
}
}
private string _loginText;
public string LoginText
{
get
{
return _loginText;
}
set
{
_loginText = value;
OnPropertyChanged("LoginText");
}
}
private string _username;
public string Username
{
get
{
return _username;
}
set
{
_username = value;
OnPropertyChanged("Username");
}
}
private string _password;
public string Password
{
get
{
return _password;
}
set
{
_password = value;
OnPropertyChanged("Password");
}
}
#endregion
public LoginViewModel()
{
//ViewModel = STARVM;
}
private void Login(object o)
{
LoginText = "";
try
{
if (Helpers.BusLayer._BL.Login(Username, Password))
{
Helpers.BusLayer.LoginSuccess = true;
MainViewModel mainVM = new MainViewModel();
LoginText = "Ok!";
}
else
{
Password = "";
LoginText = "Username or password incorrect";
}
}
catch (Exception ex)
{
Password = "";
LoginText = ex.Message;
}
}
private bool canLogin(object o)
{
return !string.IsNullOrWhiteSpace(Password) && !string.IsNullOrWhiteSpace(Username);
}
STARViewModel.cs
public STARViewModel()
{
}
登录为true时。 我想更改我的视图,但是它不这样做。
有帮助的想法吗?