【问题标题】:C# WPF ICommand interface not worksC# WPF ICommand 接口不起作用
【发布时间】:2018-08-14 11:28:40
【问题描述】:

ICommand 接口有问题。我的按钮无法正常工作。我想在填写名称和 ID 时启用它。似乎 name 和 id 没有值。至少我是这么认为的。我已尝试创建 MainWindow 的新对象并引用字段文本,但它不起作用

XAML

<Window x:Class="WpfApplication1.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:WpfApplication1"
    xmlns:m="clr-namespace:WpfApplication1"
    xmlns:mv="clr-namespace:WpfApplication1"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">

<Window.Resources>
    <m:Repository x:Key="P"/>
    <mv:ViewM x:Key="C"/>
</Window.Resources>

<Border CornerRadius="23" Background="Black" Padding="10">
    <StackPanel DataContext="{Binding Source={StaticResource C}}">

        <TextBlock Foreground="White" HorizontalAlignment="Center" Text="Main" Padding="0,5,0,0"/>
        <Separator Margin="0,10,0,0"/>
        <TextBox HorizontalAlignment="Center" Height="30" Width="200" IsReadOnly="False" Text="{Binding ID, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" TextAlignment="Center" Padding="0,5,0,0" Margin="0,4,0,0" MaxLength="20"/>
        <TextBox HorizontalAlignment="Center" Height="30" Width="200" IsReadOnly="False" Text="{Binding Name, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" TextAlignment="Center" Padding="0,5,0,0" Margin="0,4,0,0" MaxLength="20"/>
        <TextBox HorizontalAlignment="Center" Height="30" Width="200" IsReadOnly="True" Text="{Binding Full, UpdateSourceTrigger=PropertyChanged}" TextAlignment="Center" Padding="0,5,0,0" Margin="0,4,0,0" MaxLength="20"/>
        <Button  HorizontalAlignment="Center" Height="20" Width="100" Content="CHuj" Margin="0,20,0,0"
                 Command="{Binding com}" CommandParameter="{Binding Repo, UpdateSourceTrigger=PropertyChanged}"
        </Button>
    </StackPanel>

</Border>

ICommand

public class Icom : ICommand
{

    public ViewM VieM { get; set; }

    public event EventHandler CanExecuteChanged;


    public Icom(ViewM view)
    {
        this.VieM = view;
    }

    public bool CanExecute(object parameter)
    {
        Repository R = (Repository)parameter;
        if (R != null)
        {
            if (string.IsNullOrEmpty(R.Name) || (string.IsNullOrEmpty(R.ID)))
                return false;

            return true;
        }

       return false;
    }

    public void Execute(object parameter)
    {
        VieM.Wy();
    }
}

存储库

public class Repository : INotifyPropertyChanged
{

    private string _id;

    public string ID
    {
        get { return _id; }
        set { _id = value;
            OnPropertyChanged("ID");
            OnPropertyChanged("Full");
        }

    }

    private string _name;

    public string Name
    {
        get { return _name; }
        set { _name = value;
            OnPropertyChanged("Name");
            OnPropertyChanged("Full");
        }

    }

    private string _full;

    public string Full
    {
        get { return Name + " " + ID; }
        set { _full = value;
            OnPropertyChanged("Full");
        }

    }

    public Repository()
    {
        this.Name = "";
        this.ID = "";
    }


    public event PropertyChangedEventHandler PropertyChanged;

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

视图模型

 public class ViewM : INotifyPropertyChanged
{

    private string _id;

    public string ID
    {
        get { return _id; }
        set
        {
            _id = value;
            Repo = new Repository()
            {
                Full = this.Full,
                Name = this.Name,
                ID = this.ID
            };
            OnPropertyChanged("ID");
            OnPropertyChanged("Full");
        }

    }

    private string _name;

    public string Name
    {
        get { return _name; }
        set
        {
            _name = value;
            Repo = new Repository()
            {
                Full = this.Full,
                Name = this.Name,
                ID = this.ID
            };
            OnPropertyChanged("Name");
            OnPropertyChanged("Full");
        }

    }

    private string _full;

    public string Full
    {
        get { return Name + " " + ID; }
        set
        {
            _full = value;
            Repo = new Repository()
            {
                Full = this.Full,
                Name = this.Name,
                ID = this.ID                 
            };
            OnPropertyChanged("Full");
        }

    }

    private Repository _repo;

    public Repository Repo
    {
        get { return _repo; }
        set
        {
            _repo = value;
            OnPropertyChanged("Repo");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

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

    public Icom com { get; set; }

    public ViewM()
    {
        this.com = new Icom(this);
    }

    public void Wy()
    {
        MessageBox.Show("Text");
    }
}

【问题讨论】:

标签: c# wpf icommand


【解决方案1】:

您的按钮需要重新评估在您的值更改后是否可以执行。

CommandManager 在确定命令目标何时发生变化时只注意某些条件,例如键盘焦点的变化。在CommandManager 不能充分确定导致命令无法执行的条件变化的情况下, 可以调用CommandManager.InvalidateRequerySuggested Method 来强制CommandManager 引发RequerySuggested 事件。

【讨论】:

    【解决方案2】:

    我明白了

    public class Icom : ICommand
    {
    
        public ViewM VieM { get; set; }
    
        public event EventHandler CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }
    
        public void RaiseCanExecuteChanged()
        {
            CommandManager.InvalidateRequerySuggested();
        }
    
        public Icom(ViewM view)
        {
            this.VieM = view;
        }
    
        public bool CanExecute(object parameter)
        {
            Repository R = (Repository)parameter;
            if (R != null)
            {
                if (string.IsNullOrEmpty(R.ID) || (string.IsNullOrEmpty(R.Name)))
                    return false;
    
                return true;
            }
            return false;
        }
    
        public void Execute(object parameter)
        {
            VieM.Wy();
        }
    }
    

    在 ViewM 中

     private Repository _repo;
    
        public Repository Repo
        {
            get { return _repo; }
            set
            {
                _repo = value;
                OnPropertyChanged("Repo");
                com.RaiseCanExecuteChanged();
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-03
      • 1970-01-01
      • 2017-06-10
      • 1970-01-01
      相关资源
      最近更新 更多