c# - 未执行切换按钮命令

标签 c# wpf togglebutton icommand

问题是这样的。假设我有 3 个切换按钮,我只想使用 Command 一次选中一个。当一个按钮被选中时,其他按钮应该被禁用。 (我不想使用单选按钮)。 因此,我创建了这个简单的代码,但是奇怪的是,当检查按钮的单击命令未执行(未显示任何消息框)。

<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;
        }
    }
}

最佳答案

命令仅在按下按钮时执行。您需要 Hook 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() 方法。

关于c# - 未执行切换按钮命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14031261/

相关文章:

c# - 如何将静态类从 .NET 迁移到 .NET Core?

c# - 你如何从 C# 中的保存文件对话框中保存?

c# - 切换内部循环影响性能?

c# - 如何使用属性更改 TimePicker 格式

Android CompoundButton Switch jumpDrawablesToCurrentState null 崩溃

c# - linq-to-sql:SELECT 部分中的 GROUP BY 和 MIN()

wpf - 创建一个 WPF 文本框控件,如 Outlook 电子邮件收件人文本框

c# - 如何处理不支持绑定(bind)的图表?

c# - 如何从 ViewModel 更改 ToggleButton?

android - 如何将单选按钮用作切换按钮 (Android)?