Wpf中的中继命令

时间:2016-05-05 13:30:09

标签: c# wpf mvvm

我是c#的新手,并试图了解命令在mvvm架构中的工作原理。我需要做的是在点击按钮时更新一些信息。我认为我实现了接力级很好,但根本没有更新。

RelayCommand.cs

public class RelayCommand : ICommand
{
    private Action<object> execute;
    private Func<object, bool> canExecute;

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

    public RelayCommand(Action<object> execute, Func<object, bool> canExecute)
    {
        this.execute = execute;
        this.canExecute = canExecute;
    }

    public bool CanExecute(object parameter)
    {
        return this.canExecute == null || this.canExecute(parameter);
    }

    public void Execute(object parameter)
    {
        this.execute(parameter);
    }
}

MovieViewModel.cs

class MovieViewModel : INotifyPropertyChanged
{
    Movie _movie;

    private ICommand _updateCommand;
    public event PropertyChangedEventHandler PropertyChanged;

    public MovieViewModel()
    {
        _movie = new Movie
        {
            Title = "Unknown",
            Genre = "Unknown",
            Price = 11.0,
            Score = 0
        };
    }

    public Movie Movie
    {
        get
        {
            return _movie;
        }
        set
        {
            _movie = value;
        }
    }

    public string Title
    {
        get
        {
            return Movie.Title;
        }
        set
        {
            Movie.Title = value;
            RaisePropertyChanged("Title");
        }
    }

    public string Genre
    {
        get
        {
            return Movie.Genre;
        }
        set
        {
            Movie.Genre = value;
            RaisePropertyChanged("Genre");
        }
    }

    public double Price
    {
        get
        {
            return Movie.Price;
        }
        set
        {
            Movie.Price = value;
            RaisePropertyChanged("Price");
        }
    }

    public double Score
    {
        get
        {
            return Movie.Score;
        }
        set
        {
            Movie.Score = value;
            RaisePropertyChanged("Score");
        }
    }

    private void RaisePropertyChanged(string name)
    {
      PropertyChanged(this, new PropertyChangedEventArgs(name));
    }

    public ICommand UpdateCommand
    {
        get
        {
            if (_updateCommand == null)
            {
                _updateCommand = new RelayCommand(p => { updateMovie("ASD", "ZXC", 11.90, 0); }, p => true);
            }
            return _updateCommand;
        }
        set
        {
            _updateCommand = value;
        }
    }

    public Movie updateMovie(string title, string genre, double price, double score)
    {
        _movie.Title = title;
        _movie.Genre = genre;
        _movie.Price = price;
        _movie.Score = score;

        return _movie;
    }
}

按钮命令绑定

<Button x:Name="updateBtn" Content="Update" Grid.Column="1" Grid.Row="5" Width="75" Height="30" Command="{Binding UpdateCommand}"/>

1 个答案:

答案 0 :(得分:3)

尝试给予

RaisePropertyChanged("Movie"); 

谢谢@Maverik也给出了理由陈述。您没有提出PropertyChanged事件,因为您绕过了该属性并直接访问了基础字段。您应该按照它的意图使用VM。

通过VM访问模型的访问权限适用于您,只要它适用于您的VM的视图和其他绑定客户端。