Wpf 如何将命令附加到复选框的选中和取消选中?

Wpf 如何将命令附加到复选框的选中和取消选中?,wpf,xaml,data-binding,mvvm,Wpf,Xaml,Data Binding,Mvvm,在我的ViewModel中,我有两个命令: ICommand ExecuteMeOnCheck { get; } ICommand ExecuteMeOnUncheck { get; } 我希望将这些命令附加到一个复选框中,并让其中一个在选中时执行,另一个在未选中时执行。实现这一点的最佳方法是什么而不使视图中的代码混乱?您可以从checkBox控件继承这两个命令作为属性(Dependency properties)然后重写OnChecked和OnUnchecked方法,并在适当的位置触发每个命

在我的ViewModel中,我有两个命令:

ICommand ExecuteMeOnCheck { get; }
ICommand ExecuteMeOnUncheck { get; }

我希望将这些命令附加到一个复选框中,并让其中一个在选中时执行,另一个在未选中时执行。实现这一点的最佳方法是什么而不使视图中的代码混乱?

您可以从checkBox控件继承这两个命令作为属性(Dependency properties)然后重写OnChecked和OnUnchecked方法,并在适当的位置触发每个命令

我发现最简单的方法是借用一堆代码。我还认为您不想为状态更改公开两个事件。将命令设置为CheckedChangedCommand,而不是Checked和Unchecked。将CheckBox.IsChecked依赖项属性绑定到ViewModelClass上的属性。这样,当执行命令时,只需在ViewModel中检查该值

首先,从中开始使用DelegateCommand。从你的帖子上看不清楚你是不是

接下来,从该链接的下载选项卡下载

归档文件有一个名为EventBehaviorFactory的类,它使设置DependencyProperty更容易。回到上面链接的Home选项卡上,Samuel告诉您如何将XAML中的命令连接到ViewModel。这是:

<TextBox ff:TextBoxBehaviour.TextChangedCommand="{Binding TextChanged}"/>

我使用了三个类(DelegateCommand、EventBehaviorFactory和TextBoxBehavior),并在大约五分钟内让TextChanged事件在我的项目中工作

将其扩展到其他控件的模式也很容易。我设法连接了ListBox.Selection,并以同样的方式进行了更改。创建一个新的ControlBehavior类,在所有实例中用“Control”替换“TextBox”,并将DependencyProperty附加到新事件


当你的代码为空时,这是一件美好的事情

另一种类似于埃迪的解决方案:马龙·格雷奇的

您还可以使用复选框的Command属性仅附加一个命令,在其中测试复选框是否已选中


或者,您可以将IsChecked属性绑定到ViewModel的属性,并对该属性的setter中的更改作出反应。

这与Eddie的答案有关

我对它进行了一些研究,它很棒,我还创建了一个用于创建新命令的小模板。我将它作为代码片段与Sharpdevelop一起使用。只需将以下内容粘贴到代码中(例如,通过代码段):

然后用事件/命令的名称(例如MouseEnter)搜索并替换“$name”

注意,重要的是,确保为所有者选择正确的类名(在我的例子中是ControlBehavior)

我附加了一个我为常用鼠标命令创建的类,希望其他人也能共享,我们不只是一遍又一遍地实现相同的东西,我们可以通过共享节省很多时间。这里是我完整的ControlBehavior类(我只实现了我到目前为止需要的事件):


通过使用上面的模板,每个命令只花了我几秒钟的时间。

我使用“IsChecked”上的触发器来设置命令

e、 g



将代码凌乱在后面会有什么问题?这是最简单的解决方案,而且它仍然保持视图-视图-模型的分离。使用单个命令似乎不是一个好主意,因为它在命令和复选框之间引入了依赖关系。如果使用逗号,则选中绑定将是一个很好的解决方案nds是不必要的。非常感谢您的提示。我在大约3小时前遇到了上述问题,最终找到了这个问题,这正是我所需要的。我已经担心,我将不得不放弃将所有逻辑放入模型的美丽纯洁性。我能想到的唯一解决方案是将事件路由到从视图中创建模型,但这还不如这样做的一半好。
#region $nameCommand

    public static readonly DependencyProperty $nameCommand =
        EventBehaviourFactory.CreateCommandExecutionEventBehaviour(
            Control.$nameEvent, "$nameCommand", typeof (ControlBehavior));

    public static void Set$nameCommand(DependencyObject o, ICommand value)
    {
        o.SetValue($nameCommand, value);
    }

    public static ICommand Get$nameCommand(DependencyObject o)
    {
        return o.GetValue($nameCommand) as ICommand;
    }

    #endregion 
public static class ControlBehavior
    {
        #region MouseEnterCommand

        public static readonly DependencyProperty MouseEnterCommand =
            EventBehaviourFactory.CreateCommandExecutionEventBehaviour(
                Control.MouseEnterEvent, "MouseEnterCommand", typeof (ControlBehavior));

        public static void SetMouseEnterCommand(DependencyObject o, ICommand value)
        {
            o.SetValue(MouseEnterCommand, value);
        }

        public static ICommand GetMouseEnterCommand(DependencyObject o)
        {
            return o.GetValue(MouseEnterCommand) as ICommand;
        }

        #endregion

        #region MouseLeaveCommand

        public static readonly DependencyProperty MouseLeaveCommand =
            EventBehaviourFactory.CreateCommandExecutionEventBehaviour(
                Control.MouseLeaveEvent, "MouseLeaveCommand", typeof (ControlBehavior));

        public static void SetMouseLeaveCommand(DependencyObject o, ICommand value)
        {
            o.SetValue(MouseLeaveCommand, value);
        }

        public static ICommand GetMouseLeaveCommand(DependencyObject o)
        {
            return o.GetValue(MouseLeaveCommand) as ICommand;
        }

        #endregion


        #region MouseDoubleClickCommand

        public static readonly DependencyProperty MouseDoubleClickCommand = 
            EventBehaviourFactory.CreateCommandExecutionEventBehaviour(
            Control.MouseDoubleClickEvent, "MouseDoubleClickCommand", typeof (ControlBehavior));

        public static void SetMouseDoubleClickCommand(Control o, ICommand command)
        {
            o.SetValue(MouseDoubleClickCommand, command);
        }

        public static void GetMouseDoubleClickCommand(Control o)
        {
            o.GetValue(MouseDoubleClickCommand);
        }

        #endregion

        #region MouseLeftButtonDownCommand

        public static readonly DependencyProperty MouseLeftButtonDownCommand =
            EventBehaviourFactory.CreateCommandExecutionEventBehaviour(
                Control.MouseLeftButtonDownEvent, "MouseLeftButtonDownCommand", typeof (ControlBehavior));

        public static void SetMouseLeftButtonDownCommand(DependencyObject o, ICommand value)
        {
            o.SetValue(MouseLeftButtonDownCommand, value);
        }

        public static ICommand GetMouseLeftButtonDownCommand(DependencyObject o)
        {
            return o.GetValue(MouseLeftButtonDownCommand) as ICommand;
        }

        #endregion

    }
<Style x:Key="checkBoxStyle" TargetType="{x:Type CheckBox}">
      <Style.Triggers>
        <Trigger Property="IsChecked" Value="True">
          <Setter Property="Command"
                  Value="{Binding AddThingCommand}" />
        </Trigger>
        <Trigger Property="IsChecked" Value="False">
          <Setter Property="Command"
                  Value="{Binding RemoveThingCommand}" />
        </Trigger>
      </Style.Triggers>
    </Style>