【问题标题】:Toggle button command not executed未执行切换按钮命令
【发布时间】:2012-12-11 11:03:59
【问题描述】:

问题是这样的。假设我有 3 个切换按钮,我只想使用 Command 来检查一个。检查其他纽扣时,其他应禁用其他按钮。 (我不想使用单选按钮)。 所以我创建了这个简单的代码,但奇怪的是,当单击选中的按钮时,未执行命令 Execute(没有显示 MessageBox)。

<Window x:Class="ToggleButtonsProblem.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<StackPanel>
    <ToggleButton Command="{Binding ToggleCommand}" CommandParameter="{Binding RelativeSource={RelativeSource Self}}">A</ToggleButton>
    <ToggleButton Command="{Binding ToggleCommand}" CommandParameter="{Binding RelativeSource={RelativeSource Self}}">B</ToggleButton>
    <ToggleButton Command="{Binding ToggleCommand}" CommandParameter="{Binding RelativeSource={RelativeSource Self}}">C</ToggleButton>
</StackPanel>

using System;
using System.Windows;
using System.Windows.Input;
using System.Windows.Controls.Primitives;

namespace ToggleButtonsProblem {
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window {
    public MainWindow() {
        InitializeComponent();
        this.DataContext = new ViewModel();
    }
}

public class ViewModel {
    public static ICommand ToggleCommand { get { return new ToggleCommand(); } }
}

public class ToggleCommand : ICommand {
    public static bool isSomeChecked = false;
    public static ToggleButton currentCheckedButton;

    public bool CanExecute(object parameter) {
        if (currentCheckedButton == null) return true;
        return (parameter as ToggleButton).IsChecked == true;
    }

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

    public void Execute(object parameter) {
        currentCheckedButton = null;
        ToggleButton button = parameter as ToggleButton;
        MessageBox.Show(button.IsChecked.ToString());
        if (button.IsChecked == true) {
            currentCheckedButton = button;
        }
        else {                
            currentCheckedButton = null;
        }
    }
}

}

【问题讨论】:

    标签: c# wpf togglebutton icommand


    【解决方案1】:

    ToggleCommand 不应该是静态的。尝试将命令定义为属性。

    【讨论】:

    • 命令被定义为一个属性,为什么它不应该是静态的?不管是不是静态的。
    【解决方案2】:

    命令仅在按下按钮时执行。你需要钩住ToggleButton的Unchecked事件,比如这样:

    <ToggleButton Command="{Binding ToggleCommand}" Unchecked="ToggleButton_Unchecked" CommandParameter="{Binding RelativeSource={RelativeSource Self}}">A</ToggleButton>
    

    并将方法处理程序添加到代码隐藏类:

    public void ToggleButton_Unchecked(object sender, RoutedEventArgs e) {
        (sender as ToggleButton).Command.Execute(sender);
    }
    

    这应该可行,也许您可​​以找到一些更漂亮的方法来添加方法处理程序,也许作为 ToggleCommand 类的一部分。

    编辑: 尝试像这样实现 CanExecute() 方法:

    public bool CanExecute(object parameter) {            
        if (currentCheckedButton == null) return true;
        return currentCheckedButton == parameter;
    }
    

    对我来说它有效。这是我认为导致问题的原因:您单击(取消选中)按钮,因此 IsChecked 更改为 false。然后 WPF 尝试调用 Execute() 方法,但与往常一样,首先调用 CanExecute()。但是 CanExecute() 返回 false,因为检查状态已经改变,所以没有调用 Execute() 方法。

    【讨论】:

    • 正如你所说的“命令仅在按下按钮时执行。”这也意味着未选中时。尝试将 ToggleCommand 的 CanExecute 更改为“返回 true”,您会看到 Execute 方法在选中和未选中时被触发。顺便说一句,我认为同时定义事件和命令是不好的做法(更不用说我需要命令来实现 MVVM):)
    • 编辑:是的,这是有效的,我认为你的解释是对的 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-23
    • 2013-06-23
    • 2016-09-05
    • 1970-01-01
    • 2020-07-20
    相关资源
    最近更新 更多