Reputation: 2474
I'm struggling as to how to toggle switch between Toggle Buttons. What I want to happen is if Yes Button is pressed, then the No Button will be unchecked and vice versa. Right now, it is allowing the user to select both which I do not want to happen. Also, the Unchecked is used in the wrong format and it says it must only appear to the left hand side of += or -=, noTBU.Unchecked = true; & yesTBU.Unchecked = true; I'm not sure I understand this? I thought it returns true or false?
// 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?";
}
}
Upvotes: 0
Views: 3000
Reputation: 89325
Unchecked
is an event, not common property. +=
and -=
are respectively notation to attach and detach event handler from event.
Instead of setting Unchecked = true;
I think you want to set IsChecked = false;
:
noTBU.IsChecked = false;
//instead of noTBU.Unchecked = true;
UPDATE :
If you use same event handler to handle Tap
event of both ToggleButtons, you can get currently tapped button from sender
parameter. So, how about this way :
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;
}
}
Upvotes: 1