【问题标题】:MVVM Button can't be pressedMVVM 按钮无法按下
【发布时间】:2018-03-22 07:15:14
【问题描述】:

我有三个简单的文本框和两个按钮。在文本框中是名称、姓氏和全名。如果我按下第一个按钮(简单命令),它应该调试一个“你好”。此按钮运行良好,但第二个按钮(参数命令)绑定到全名,应该调试人员。但是按钮被禁用,我找不到问题。

个人类:

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));
    }
}

参数命令类:

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。但如您所见,姓氏不为空。

【问题讨论】:

标签: c# wpf mvvm


【解决方案1】:

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

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

如果CanExecute 方法中的条件发生变化,CommandManager 类负责引发事件。我想它现在应该可以工作了。

【讨论】:

    猜你喜欢
    • 2016-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-07
    • 2020-05-16
    • 2016-05-07
    • 1970-01-01
    相关资源
    最近更新 更多