如果触发事件来自另一个框架中的控件,则不会禁用按钮

时间:2019-05-08 17:29:56

标签: c# wpf icommand

我试图在文本框为空时通过命令禁用按钮。
接口的结构为:
MainWindow.xaml-包含需要禁用的按钮。
page.xaml-在主窗口的框架中可视化的页面。

XAML:

<Window x:Class="CommandButtons.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:CommandButtons"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Window.DataContext>
        <local:WindowVM/>
    </Window.DataContext>
    <Grid>
        <Button Content="Button" Command="{Binding LaunchButtonAction}"  HorizontalAlignment="Left" Margin="555,80,0,0" VerticalAlignment="Top" Width="75" Height="40"/>
        <TextBox HorizontalAlignment="Left"  Height="65" Margin="80,120,0,0" TextWrapping="Wrap" Text="{Binding TextSource,UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Top" Width="245"/>
    </Grid>
</Window>

视图模型:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;

namespace CommandButtons
{
    class WindowVM
    {
        private string textSource;

        public string TextSource
        {
            get { return textSource; }
            set
            {
                textSource = value;
                if(textSource.Length==0)
                {
                    canExecute = false;
                }else
                {
                    canExecute = true;
                }
            }
        }


        private ICommand launchButtonAction;
        public ICommand LaunchButtonAction
        {
            get { return launchButtonAction; }
            set { launchButtonAction = value; }
        }

        private ICommand enableDisable;

        public ICommand EnableDisable
        {
            get { return enableDisable; }
            set { enableDisable = value; }
        }


        private bool canExecute = false;
        public bool CanExecute
        {
            get { return canExecute; }
            set
            { if(canExecute==value)
                { return; }
                canExecute = value;
            }
        }

        public WindowVM()
        {
            LaunchButtonAction = new RelayCommand(ShowMessage, param => this.canExecute);
           // EnableDisable = new RelayCommand(SwitchOnOff);
        }


        private void ShowMessage(object obj)
        {
            MessageBox.Show("enabled");
        }
    }
}

RelayComand:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace CommandButtons
{
    class RelayCommand : ICommand
    {
        private Action<object> execute;
        private Predicate<object> canExecute;
        private event EventHandler canExecuteChangedInternal;

        public RelayCommand(Action<object> execute)
            :this(execute, DefaultCanExecute)
        {

        }

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

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

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

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

        public void OnCanExecuteChanged()
        {
            EventHandler handler = this.canExecuteChangedInternal;
            if(handler!=null)
            {
                handler.Invoke(this, EventArgs.Empty);
            }
        }

        public void Destroy()
        {
            this.canExecute = _ => false;
            this.execute = _ => { return; };
        }

        private static bool DefaultCanExecute(object parameter)
        {
            return true;
        }
    }
}

我真正发现的是,如果我将命令附加到页面中的一个按钮上-该按钮将被禁用,而第一个按钮将继续保持启用状态。

非常困惑。 任何帮助表示赞赏!

0 个答案:

没有答案