无法按下MVVM按钮

时间:2018-03-22 07:15:15

标签: c# wpf mvvm

我有三个简单的TextBoxes和两个Buttons。在TextBox中有名称,姓氏和全名。如果我按下第一个按钮(简单命令),它应该调试一个"你好"。此按钮运行良好,但第二个按钮(参数命令)绑定到fullname并应调试此人。但该按钮被禁用,我无法找到问题。

与人类:

class Person : INotifyPropertyChanged
{
    public Person() {
        Name = "Max";
        Lastname = "Mustermann";
        Fullname = Name + " " + Lastname;
    }
    private string name;

    public string Name {
        get { return name; }
        set {
            name = value;
            OnPropertyChanged("Name");
            OnPropertyChanged("Fullname");
        }
    }

    private string lastname;

    public string Lastname
    {
        get { return lastname; }
        set
        {
            lastname = value;
            OnPropertyChanged("Lastame");
            OnPropertyChanged("Fullname");
        }
    }

    private string fullname;

    public string Fullname
    {
        get { return name +" "+ lastname; }
        set
        {
            fullname = value;
            OnPropertyChanged("Fullname");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void OnPropertyChanged(String propertyName) {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}

ParameterCommand级:

public class ParameterCommand : ICommand
{
    public ViewModelBase ViewModel { get; set; }

    public ParameterCommand(ViewModelBase viewModel) {
        this.ViewModel = viewModel;
    }
    public event EventHandler CanExecuteChanged;

    public bool CanExecute(object parameter)
    {
        if(parameter != null) {
            var s = parameter as String;
            if (String.IsNullOrEmpty(s))
                return false;

            return true;
        }
        return false;
    }

    public void Execute(object parameter)
    {
        this.ViewModel.ParameterMethod(parameter as String);
    }
}

XAML中的主窗口:

<Window x:Class="MVVMPerson.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:MVVMPerson"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:m="clr-namespace:MVVMPerson.Models"
    xmlns:vm ="clr-namespace:MVVMPerson.ViewModels"
    mc:Ignorable="d"
    Title="MainWindow" Height="450" Width="800">

<Window.Resources>
    <m:Person x:Key="person"/>
    <vm:ViewModelBase x:Key="viewModel"/>
</Window.Resources>


<Grid DataContext="{Binding Source={StaticResource person}}">
    <StackPanel VerticalAlignment="Center"
                HorizontalAlignment="Center">
        <TextBox Width="150" Text="{Binding Name, Mode=TwoWay}"/>
        <TextBox Width="150" Text="{Binding Lastname, Mode=TwoWay}"/>
        <TextBox Width="150" Text="{Binding Fullname}" SelectionOpacity="5"/>
        <Button Content="simple command" Command="{Binding Path=SimpleCommand, Source={StaticResource viewModel}}"/>
        <Button Content="parameter command" Command="{Binding ParameterCommand, Source={StaticResource viewModel}}" 
                CommandParameter="{Binding Fullname}" />
    </StackPanel>
</Grid>

ViewModelBase级:

    public class ViewModelBase
{
    public SimpleCommand SimpleCommand { get; set; }
    public ParameterCommand ParameterCommand { get; set; }

    public ViewModelBase() {
        this.SimpleCommand = new SimpleCommand(this);
        this.ParameterCommand = new ParameterCommand(this);
    }

    public void SimpleMethod() {
        Debug.WriteLine("hello");
    }

    public void ParameterMethod(string person) {
        Debug.WriteLine(person);
    }
}

问题是,ParameterCommand中的CanExecute Methode将在

处中断
if(parameter != null)

并返回false。但正如您所看到的,姓氏不是空的。

1 个答案:

答案 0 :(得分:1)

您的活动CanExecuteChanged缺少一些必要的代码行。它应该是这样的:

public event EventHandler CanExecuteChanged {
        add {
            CommandManager.RequerySuggested += value;
        }
        remove {
            CommandManager.RequerySuggested -= value;
        }
    }

如果CanExecute方法中的条件已更改,则类CommandManager负责引发事件。我想现在应该可以了。