2012-12-25 84 views
3

問題是這樣的。假設我有3個切換按鈕,並且我希望只有一個正在使用Command進行檢查。當一個按鈕被選中時,其他人應該被禁用。 (我不想使用單選按鈕)。 所以我創建了這個簡單的代碼,但奇怪的是,當點擊選中按鈕命令執行沒有執行(沒有顯示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; 
     } 
    } 
} 

}

回答

1

僅當按下按鈕時才執行命令。您需要掛鉤的切換按鈕的未經檢查的情況下,例如像這樣:

<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()方法。

+0

正如你所說的:「只有按下按鈕時才執行命令。」這也意味着未經檢查。嘗試將ToggleCommand的CanExecute更改爲「返回true」,並且您看到Execute方法在選中時被觸發並且未選中。順便說一下,我認爲定義事件和命令在同一時間是不好的做法(更不用說我需要命令來實現MVVM):) –

+0

編輯:是的,這是工作,我認爲你是正確的與你的解釋:) –

0

ToggleCommand不應該是一成不變的。嘗試將該命令定義爲屬性。

+0

命令被定義爲一個屬性,爲什麼它不應該是靜態的?它是否是靜態的並不重要。 –

+0

你是對的,它的工作! – user1064519