【发布时间】:2015-10-15 00:31:54
【问题描述】:
我正在尝试使用 Caliburns 'Can' 约定在我的视图上启用一个按钮以进行视图模型属性评估。
查看(摘录)
<PasswordBox PasswordChanged="PasswordBox_OnPasswordChanged" Grid.Row="1" Grid.Column="1" />
...
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
<Button Content="Cancel" cal:Message.Attach="[Event Click] = [Action Cancel]" />
<Button Content="Login" cal:Message.Attach="[Event Click] = [Action Login]" />
</StackPanel>
代码隐藏
private void PasswordBox_OnPasswordChanged(object sender, RoutedEventArgs e)
{
if (DataContext != null)
((dynamic) DataContext).Password = ((PasswordBox) sender).Password;
}
视图模型
public class LoginSplashViewModel : Screen
{
private string _username;
private string _password;
public string Username
{
get { return _username; }
set
{
_username = value;
NotifyOfPropertyChange();
}
}
public string Password
{
get { return _password; }
set
{
_password = value;
NotifyOfPropertyChange();
}
}
public LoginSplashViewModel()
{
DisplayName = "Login";
}
public bool CanLogin()
{
return !string.IsNullOrEmpty(_username) ||
!string.IsNullOrEmpty(_password);
}
public void Login()
{
TryClose(true);
}
public void Cancel()
{
TryClose(false);
}
}
但是'CanLogin()' 方法只被触发一次(当将视图模型绑定到视图时),并且永远不会再次触发,因此按钮保持禁用状态。
我错过了什么吗?
【问题讨论】:
标签: c# .net wpf mvvm caliburn.micro