2014-02-25 36 views
0

我正在努力如何切換切換按鈕之間的切換。我想要發生的情況是,如果按下了「是」按鈕,則「否」按鈕將被取消選中,反之亦然。現在,它允許用戶選擇我不想發生的事情。另外,Unchecked的格式不正確,它表示它只能出現在+ =或 - =的左邊,noTBU.Unchecked = true; & yesTBU.Unchecked = true;我不確定我明白這一點?我認爲它返回真或假?如何正確切換切換按鈕與是/否?

// method to handle all Toggle Button tap events 
    private void ToggleButton_Tap(object sender, System.Windows.Input.GestureEventArgs e) 
    { 
     if(yesTBU.IsPressed) 
     { 
      // add selected value to text 
      // disable noTBU toggle 
      //noTBU.IsChecked = false; 
      yesTBU.IsChecked = true; 
      noTBU.Unchecked = true; 

      if(noTBU.IsPressed) 
      { 
       yesTBU.IsChecked = false; 
      } 
     } 
     if(noTBU.IsPressed) 
     { 
      // add selected value to text 
      // disable yesTBU toggle 
      //yesTBU.IsChecked = false; 
      noTBU.IsChecked = true; 
      yesTBU.Unchecked = true; 

      if(yesTBU.IsPressed) 
      { 
       noTBU.IsChecked = false; 
      } 
     } 
     // makes dynamic changes immediately in UI 
     if(yesTBU.IsChecked == false && noTBU.IsChecked == false) 
     { 
      // message box and text font change or Not Specified 
      disabilityTBL.Text = "Are you disabled? *"; 
      disabilityTBL.Foreground = new SolidColorBrush(Colors.Red); 
      disabilityTBL.FontWeight = FontWeights.Bold; 
     } 
     else 
     { 
      // set back to default layout 
      this.disabilityTBL.ClearValue(TextBlock.ForegroundProperty); 
      this.disabilityTBL.ClearValue(TextBlock.FontWeightProperty); 
      this.disabilityTBL.Text = "Are you disabled?"; 
     } 
    } 

回答

1

Unchecked是一個事件,不是普通的屬性。 +=-=分別表示附加和從事件中分離事件處理程序。

而不是設置Unchecked = true;我覺得你要設置IsChecked = false;

noTBU.IsChecked = false; 
//instead of noTBU.Unchecked = true; 

UPDATE:

如果使用相同的事件處理程序來處理Tap事件雙方的ToggleButtons,你可以得到當前抽頭從sender參數的按鈕。那麼,怎麼樣這樣:

private void ToggleButton_Tap(object sender, System.Windows.Input.GestureEventArgs e) 
{ 
    //if currently tapped button is yesTBU 
    if(sender == yesTBU) 
    { 
     //set noTBU to opposite value of yesTBU 
     noTBU.IsChecked = !yesTBU.IsChecked; 
    } 
    //else if currently tapped button is noTBU 
    else 
    { 
     //set yesTBU to opposite value of noTBU 
     yesTBU.IsChecked = !noTBU.IsChecked; 
    } 
} 
+1

謝謝一堆!我會記住這個對象發送者來相應地處理事件。 – TheAmazingKnight