ICommand不起作用

时间:2015-08-29 09:54:46

标签: c# wpf xaml mvvm icommand

我从MVVM模式开始,我的按钮命令出现问题。我有一个窗口,其中包含用于登录的TextBox和一个密码为PasswordBox的自定义DependencyProperty(我知道任何密码都不应保存在内存中,但它只是为了好玩,所以不要感到沮丧:))。还有一个按钮,我希望当且仅当登录名和密码都不为空时才启用它。但是我的命令无法正常工作。这是代码:

LoginWindow xaml:

<Window x:Class="WpfMVVMApplication1.LoginWindow"
    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:WpfMVVMApplication1"
    xmlns:vm="clr-namespace:WpfMVVMApplication1.ViewModel"
    mc:Ignorable="d"
    Title="Login" WindowStartupLocation="CenterScreen" ResizeMode="NoResize"
    Width="250" Height="150">
<Window.DataContext>
    <vm:LoginViewModel />
</Window.DataContext>
<Window.Resources>
    <!-- here some styles for controls -->
</Window.Resources>
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="1.5*"/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="*"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <TextBlock Text="Login:"/>
    <TextBlock Text="Password:" Grid.Row="1"/>
    <TextBox Text="{Binding Login, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"/>
    <local:MyPasswordBox Grid.Row="1" PasswordText="{Binding Password, Mode=TwoWay}"/>
    <Button Command="{Binding SaveUserCommand}"/>
</Grid>

我的LoginViewModel类

public class LoginViewModel : INotifyPropertyChanged
{
    public LoginViewModel()
    {
        SaveUserCommand = new Command(x => SaveUser(), x => { return !string.IsNullOrEmpty(Login) && !string.IsNullOrEmpty(Password); });
    }

    public event PropertyChangedEventHandler PropertyChanged;

    public ICommand SaveUserCommand { get; private set; }

    private string login;
    private string password;
    public string Login
    {
        get
        {
            return login;
        }
        set
        {
            login = value;
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Login"));
        }
    }

    public string Password
    {
        get
        {
            return password;
        }
        set
        {
            password = value;
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Password"));
        }
    }

    public void SaveUser() { }
}

同样MyPasswordBox类可能有用:

<UserControl x:Class="WpfMVVMApplication1.MyPasswordBox"
         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:WpfMVVMApplication1"
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
<Grid>
    <PasswordBox x:Name="password" PasswordChanged="password_PasswordChanged"/>
</Grid>

public partial class MyPasswordBox : UserControl
{
    public MyPasswordBox()
    {
        InitializeComponent();
        PasswordText = "";
    }

    public static readonly DependencyProperty PasswordTextProperty =
        DependencyProperty.Register("PasswordText", typeof(string), typeof(MyPasswordBox), new FrameworkPropertyMetadata(""));

    public string PasswordText
    {
        get { return (string)GetValue(PasswordTextProperty); }
        set { SetValue(PasswordTextProperty, value); }
    }

    private void password_PasswordChanged(object sender, RoutedEventArgs e)
    {
        PasswordText = password.Password;
    }
}

我为了检查SaveUserCommand CanExecute方法的结果而编写了一些单元测试,它们都通过了。此外,我在Visual Studio中运行调试,在LoginPassword属性的setter中添加断点,并且它们都已正确设置。

我已经用尽了我可能犯错的想法。有人能帮助我吗?

我觉得我没有正确通知命令有关更改,但我不知道该怎么做

1 个答案:

答案 0 :(得分:1)

为了让WPF获取对命令是否可以执行的更改,需要触发命令的CanExecuteChanged事件。

登录或密码更改时,您需要触发此事件。您还没有显示您的课程Command的来源,但它需要有一个方法来触发其CanExecuteChanged事件。然后,只需从LoginPassword属性设置器中调用此方法。