admin管理员组文章数量:1318582
I am using MVVM light (GalaSoft.MvvmLight) in my WPF application.
My View
<base:BaseWindow x:Class="Hub.Kiosk.View.WindowLogin"
xmlns=";
xmlns:x=";
xmlns:d=";
xmlns:mc=";
xmlns:local="clr-namespace:Hub.Kiosk.View"
xmlns:base="clr-namespace:Hub.KIOSK.Base"
mc:Ignorable="d"
Title="WindowLogin" Height="450" Width="800">
<Grid>
<StackPanel VerticalAlignment="Center" HorizontalAlignment="Center" Orientation="Vertical">
<TextBlock Text="User Name"/>
<TextBox Text="{Binding User.UserName, Mode=TwoWay}" Height="30" Width="200" Style="{StaticResource TextBox-Rounded }"/>
<TextBlock Text="Password"/>
<TextBox Text="{Binding User.Password, Mode=TwoWay}" Height="30" Width="200" Style="{StaticResource TextBox-Rounded }"/>
<Rectangle Height="20"/>
<Button Content="Login" Command="{Binding LoginCommand}"/>
</StackPanel>
</Grid>
</base:BaseWindow>
ViewModel
public class WindowLoginViewModel : ViewModelBase
{
private User _user;
public User User
{
get => _user;
set => Set(ref _user, value);
}
#region COMMANDS
public RelayCommand LoginCommand { get; private set; }
#endregion
public WindowLoginViewModel()
{
User =new User();
// Initialize RelayCommand
LoginCommand = new RelayCommand(LoginProcess, CanLogin);
}
private bool CanLogin()
{
return !string.IsNullOrEmpty(User.UserName)
&& !string.IsNullOrEmpty(User.Password);
}
private void LoginProcess()
{
Console.WriteLine($"Username: {User.UserName} | Password: {User.Password}");
}
}
Model
public class User : ObservableObject
{
private string _userName;
private string _password;
public string UserName
{
get { return _userName; }
set => Set(ref _userName, value);
}
public string Password
{
get => _password;
set => Set(ref _password, value);
}
}
I need to enable the Login button, when user entered both username & password.
private bool CanLogin()
{
return !string.IsNullOrEmpty(User.UserName)
&& !string.IsNullOrEmpty(User.Password);
}
But this CanLogin only fired at the time of loading, after that when I changed value in the textbox, it is not trigger. I'm using .Net Framework 4.5.2 & galasoft mvvm light.
I tried with a simple string field called Comment.
<TextBlock Text="Comment"/>
<TextBox Text="{Binding Comment, Mode=TwoWay}" Height="30" Width="200" Style="{StaticResource TextBox-Rounded }"/>
ViewModel
private string _comment;
public string Comment
{
get => _comment;
set => Set(ref _comment, value);
}
private bool CanLogin()
{
//return !string.IsNullOrEmpty(User.UserName)
//&& !string.IsNullOrEmpty(User.Password);
return !string.IsNullOrEmpty(Comment);
}
Still same issue. If I hard code CanLogin as return true, then when LoginProcess fired getting newly entered username & password.
if I set like below, working
public string Comment
{
get => _comment;
set
{
if (Comment != value)
{
_comment = value;
RaisePropertyChanged(); // Notify UI
LoginCommand.RaiseCanExecuteChanged(); // Re-check CanExecute
}
}
}
本文标签: WPF MVVM light Relaycommand can execute not trigger when field value changedStack Overflow
版权声明:本文标题:WPF MVVM light Relaycommand can execute not trigger when field value changed - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742047263a2417861.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论