首页 > 解决方案 > C# 和 WPF 中的命令

问题描述

我有非常简单的代码,但它不起作用。

我有一个命令:

public class SetYesCommand : ICommand , INotifyPropertyChanged
    {
        public event EventHandler CanExecuteChanged;
        public event PropertyChangedEventHandler PropertyChanged;

        ViewModelBase view = new ViewModelBase();

        public bool CanExecute(object parameter)
        {
            return true;
        }

        public void Execute(object parameter)
        {
            view.Sname = "Yes";
            MessageBox.Show("Command works");
            onpropertychanged("Sname");
        }

        private void onpropertychanged(string propertyname)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyname));
            }
        }
    }
}

这是我的基本视图模型类:

 public class ViewModelBase : INotifyPropertyChanged
    {
        private  string _s;
        public  string Sname {
            get { return _s; }
            set { _s = value;
                onPropertyChanged("Sname");
            }

        }

        private void onPropertyChanged(string propertyname)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyname));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }
}

这是我的 XAML 代码:

<Window x:Class="Test.MainWindow"
        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:Test"
        mc:Ignorable="d"
        xmlns:viewmodel="clr-namespace:Test.ViewModel"
         xmlns:commands="clr-namespace:Test.ViewModel.Commands"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <viewmodel:ViewModelBase  x:Key="base" ></viewmodel:ViewModelBase>
        <viewmodel:Converters  x:Key="convert" ></viewmodel:Converters>
        <commands:SetYesCommand x:Key="setyes"/>
    </Window.Resources>
    <StackPanel>
        <Button Width="100" Height="30" Command="{StaticResource setyes}"/>
        <TextBox DataContext="{StaticResource base}" Text="{Binding Sname , Mode=TwoWay}"/>
        <CheckBox DataContext="{StaticResource base}" IsChecked="{Binding Sname , Mode=TwoWay , Converter={StaticResource convert}}"  />
        <TextBox />
    </StackPanel>
</Window>

正如您所看到的,我将 UI 中的两个元素绑定到我的视图模型基类中的字符串属性,并且我在 UI 中为我的按钮定义了一个命令。该命令有效,因为如您所见,我使用消息框对其进行了检查,但我的 UI 中其他两个元素(文本框和复选框)的值没有改变。

标签: c#wpf

解决方案


您正在命令中创建一个新实例ViewModelBase。应该创建命令的是视图模型,而不是相反。

一个典型的ICommand接口实现一般接受一个Action<object>要执行的和一个Predicate<object>决定是否执行命令的:

public class SetYesCommand : ICommand
{
    public event EventHandler CanExecuteChanged;
    private readonly Action<object> _execute;
    private readonly Predicate<object> _canExecute;

    public SetYesCommand(Action<object> execute, Predicate<object> canExecute)
    {
        _execute = execute;
        _canExecute = canExecute;
    }

    public bool CanExecute(object parameter)
    {
        if (_canExecute == null)
            return true;
        return _canExecute(parameter);
    }

    public void Execute(object parameter)
    {
        if (_execute != null)
            _execute(parameter);
    }
}

然后在视图模型中实现逻辑:

public class ViewModelBase : INotifyPropertyChanged
{
    private string _s;
    public string Sname
    {
        get { return _s; }
        set
        {
            _s = value;
            onPropertyChanged("Sname");
        }

    }

    public ViewModelBase()
    {
        TheCommand = new SetYesCommand(OnCommandExecuted, null);
    }

    private void OnCommandExecuted(object parameter)
    {
        Sname = "Yes";
        MessageBox.Show("Command works");
    }

    public ICommand TheCommand { get; private set; }

    private void onPropertyChanged(string propertyname)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyname));
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
}

在视图中,您绑定到返回ICommand实现实例的视图模型的命令属性:

<Button Width="100" Height="30" DataContext="{StaticResource base}" Command="{Binding TheCommand}"/>

推荐阅读