C# 如何使用是/否在切换按钮之间正确切换?
我很难在切换按钮之间切换。我想发生的是,如果按下“是”按钮,那么“否”按钮将被取消选中,反之亦然。现在,它允许用户选择我不希望发生的两种情况。此外,Unchecked的格式错误,它表示它只能出现在+=或-=,noTBU的左侧。Unchecked=true;&yesTBU.Unchecked=true;我不确定我是否明白这一点?我认为它返回的是真还是假C# 如何使用是/否在切换按钮之间正确切换?,c#,wpf,windows-phone-8,visual-studio-2013,togglebutton,C#,Wpf,Windows Phone 8,Visual Studio 2013,Togglebutton,我很难在切换按钮之间切换。我想发生的是,如果按下“是”按钮,那么“否”按钮将被取消选中,反之亦然。现在,它允许用户选择我不希望发生的两种情况。此外,Unchecked的格式错误,它表示它只能出现在+=或-=,noTBU的左侧。Unchecked=true;&yesTBU.Unchecked=true;我不确定我是否明白这一点?我认为它返回的是真还是假 // method to handle all Toggle Button tap events private void Tog
// 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?";
}
}
未选中
是一个事件,而不是公共属性+=
和-=
分别是将事件处理程序与事件附加和分离的符号
而不是设置Unchecked=true
我想您应该设置IsChecked=false代码>:
noTBU.IsChecked = false;
//instead of noTBU.Unchecked = true;
更新:
如果您使用相同的事件处理程序来处理两个切换按钮的Tap
事件,则可以从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;
}
}
非常感谢!我将记住对象发送器,以便相应地操作事件。