2014-10-31 74 views
1

有一個。我如何設置所有單選器isChecked屬性設置爲false

最初他們都設置爲false

但是,一旦我點擊其中的一個,一個停留在任何時候都如此。

我們的要求是,它們都可以被設置爲false。

我試過的東西

<MultiDataTrigger> 
    <MultiDataTrigger.Conditions> 
     <Condition Binding="{Binding IsChecked, 
        RelativeSource={RelativeSource Self}}" Value="True"/> 
     <Condition Binding="{Binding IsPressed, 
        RelativeSource={RelativeSource Self}}" Value="True"/> 
    </MultiDataTrigger.Conditions> 
    <Setter Property="IsChecked" Value="False"/> 
</MultiDataTrigger> 

但它不工作。我知道我可以使用ToggleButton,但我的RadioButtons一次只能檢查一個。

這甚至可能嗎?

回答

0

你的解決方案僅當鼠標按鈕被按下實際(抱着嘗試它,你會看到)。 想到的最簡單的方法是改變RadioButton有點滿足我們的需要:如果再次點擊

public class UncheckableRadioButton : RadioButton 
{ 
    protected override void OnToggle() 
    { 
     if (IsChecked == true) 
     IsChecked = false; 
     else base.OnToggle(); 
    } 
} 

Theese定製無線電將取消。

+0

不是我解決我的問題的方式,但對於未來的讀者看。 – DeMama 2014-10-31 12:38:52

+0

@DeMama,你是否介意分享你的解決方案(通過編輯或單獨的答案)?這可能對其他人非常有用,對我們來說相當有趣,誰回答了這個問題=) – icebat 2014-10-31 12:48:30

+0

在這裏,你走了。不過請注意,這個解決方案對於我所需要的非常具體。 – DeMama 2014-10-31 13:10:54

0

我可以馬上告訴你,風格不會解決你的問題,因爲一旦用戶設置了DependencyProperty的值,它將忽略風格設置器,你應該閱讀Dependency Property Value Precedence

在另一方面,你可以嘗試處理你自己的鼠標點擊,當一個單選按鈕器isChecked =真,就像這樣:

 <RadioButton GroupName="AAA" PreviewMouseLeftButtonDown="RadioButton_PreviewMouseLeftButtonDown"/> 

和:

private void RadioButton_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) 
    { 
     RadioButton radioButton = sender as RadioButton; 

     if (radioButton != null && radioButton.IsChecked.GetValueOrDefault()) 
     { 
      radioButton.IsChecked = false; 
      e.Handled = true; 
     } 
    } 

我覺得這樣做訣竅。

+0

如果使用鍵盤切換收音機會怎麼樣? =) – icebat 2014-10-31 12:34:33

+0

我想需要你的用戶PreviewKeyDown爲好,用相同的代碼,同時檢查如果按下的鍵是空格鍵... – Yoav 2014-10-31 12:41:23

0

由於icebat要求,我會後我的解決方案:

我只是創造了一個事件處理程序,我RadioButtons,我比較我的類中定義的某些值:

private int PopupID; 
private bool IsPopupActive; 
private void RadioButton_Click(object sender, RoutedEventArgs e) 
{ 
    RadioButton rb = (RadioButton)sender; 
    string s = (string)rb.Content; 
    int id = int.Parse(s); 

    if (PopupID == id && IsPopupActive == true) 
    { 
     foreach (RadioButton rbb in PopupGrid.Children.OfType<RadioButton>()) 
      rbb.SetValue(RadioButton.IsCheckedProperty, false); 

     IsPopupActive = false; 
    } 
    else if (PopupID != id || IsPopupActive == false) 
    { 
     foreach (RadioButton rbb in PopupGrid.Children.OfType<RadioButton>() 
       .Where(x => x != rb)) 
      rbb.SetValue(RadioButton.IsCheckedProperty, false); 

     IsPopupActive = true; 
     PopupID = id; 
    } 
} 

不是一個大風扇這樣的代碼隱藏解決方案,但它的工作原理。

相關問題