none
WPF ICommand不起作用 RRS feed

  • 问题

  • 界面一个TextBox和一个按钮,当TextBox的Text不为空时,Button可以点击,并情况TextBox的内容。

    <Grid>
        <StackPanel Orientation="Vertical">
            <TextBox Text="{Binding InputText, Mode=TwoWay}" Width="200" Height="30" />
            <Button Content="Button" Command="{Binding TextClearCommand}" Width="200" Height="30"/>
        </StackPanel>
    </Grid>
    RelayCommand.cs
    public class RelayCommand: ICommand
    {
        readonly Action<object> execute;
        readonly Predicate<object> canExecute;
    
        public RelayCommand(Action<object> execute, Predicate<object> canExecute)
        {
            this.execute = execute;
            this.canExecute = canExecute;
        }
    
        public event EventHandler CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }
    
        public bool CanExecute(object parameter)
        {
            return canExecute == null ? true : canExecute(parameter);
        }
    
        public void Execute(object parameter)
        {
            execute(parameter);
        }
    }

    MainWindow.cs

    public partial class MainWindow : Window, INotifyPropertyChanged
    {
        public MainWindow()
        {
            InitializeComponent();
    
    
            this.DataContext = this;
        }
    
        public event PropertyChangedEventHandler PropertyChanged;
        private string mInputText = "";
        public string InputText
        {
            get { return mInputText; }
            set
            {
                mInputText = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(nameof(InputText)));
                }
            }
        }
    
        private RelayCommand _TextClearCommand = null;
    
        public ICommand TextClearCommand
        {
            get
            {
                if (_TextClearCommand == null)
                {
                    _TextClearCommand = new RelayCommand(Execute, CanExecute);
                }
                return _TextClearCommand;
            }
        }
    
        private void Execute(object p)
        {
            MessageBox.Show("object");
        }
    
        private bool CanExecute(object p)
        {
            if (string.IsNullOrEmpty(InputText))
            {
                return false;
            }
            return true;
        }
    }


    2017年8月14日 15:25

答案


  • Hi  oneonce,

    请参照下面的方法。使用IValueConverter来判定是否button可用。

    XAML:

    <Window.Resources>
            <local:buttonenableConverterTest x:Key="buttonenableConverterTest"/>
        </Window.Resources>
        <Grid>
            <StackPanel Orientation="Vertical">
                <TextBox x:Name="textBox" Text="{Binding InputText, Mode=TwoWay}" Width="200" Height="30" />
                <Button Content="Button" Command="{Binding TextClearCommand}" IsEnabled="{Binding Path=Text,ElementName=textBox, Converter={StaticResource buttonenableConverterTest}  }" Width="200" Height="30"/>
            </StackPanel>
        </Grid>

    XAML.CS:

    public partial class WPFICommandDem : Window, INotifyPropertyChanged { public WPFICommandDem() { InitializeComponent(); this.DataContext = this; } public event PropertyChangedEventHandler PropertyChanged; private string mInputText = ""; public string InputText { get { return mInputText; } set { mInputText = value; if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(nameof(InputText))); } } } private RelayCommand _TextClearCommand = null; public ICommand TextClearCommand { get { if (_TextClearCommand == null) { _TextClearCommand = new RelayCommand(Execute, CanExecute); } return _TextClearCommand; } } private void Execute(object p) { MessageBox.Show("object");

      textBox.Text = ""; } private bool CanExecute(object p) { //if (string.IsNullOrEmpty(InputText)) //{ // return false; //} return true; } } public class RelayCommand : ICommand { readonly Action<object> execute; readonly Predicate<object> canExecute; public RelayCommand(Action<object> execute, Predicate<object> canExecute) { this.execute = execute; this.canExecute = canExecute; } public event EventHandler CanExecuteChanged { add { CommandManager.RequerySuggested += value; } remove { CommandManager.RequerySuggested -= value; } } public bool CanExecute(object parameter) { return canExecute == null ? true : canExecute(parameter); } public void Execute(object parameter) { execute(parameter); } } public class buttonenableConverterTest : IValueConverter { public object Convert(object value, Type typeTarget, object param, CultureInfo culture) { if ((string)value != "") { return true; } else { return false; } } public object ConvertBack(object value, Type typeTarget, object param, CultureInfo culture) { throw new NotImplementedException(); } }


    Best Regards,

    Yohann Lu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    2017年8月15日 1:50
    版主