将 ToggleSwitch 设置为开或关

问题描述 投票:0回答:1
c# uwp uwp-xaml toggleswitch
1个回答
0
投票

根据您的描述,您应该最多选择三个切换开关之一。

建议使用 Toggled 事件而不是

PointerReleased
事件。判断事件中当前ToggleSwitch是否为
IsOn
,然后关闭另外两个ToggleSwitch。

<ToggleSwitch
x:Name="onlineToggleSwitch"
VerticalAlignment="Center"
FlowDirection="LeftToRight"
OffContent=""
OnContent=""
  Toggled="onlineToggleSwitch_Toggled">
</ToggleSwitch>

<ToggleSwitch
x:Name="offlineToggleSwitch"
VerticalAlignment="Center"
FlowDirection="LeftToRight"
OffContent=""
OnContent=""
    Toggled="offlineToggleSwitch_Toggled">
</ToggleSwitch>

<ToggleSwitch
x:Name="maxToggleSwitch"
VerticalAlignment="Center"
FlowDirection="LeftToRight"
OffContent=""
OnContent=""
    Toggled="maxToggleSwitch_Toggled">
</ToggleSwitch>

private void onlineToggleSwitch_Toggled(object sender, RoutedEventArgs e)
 {
     
     if (onlineToggleSwitch.IsOn == true)
     {
         offlineToggleSwitch.IsOn = false;
         maxToggleSwitch.IsOn = false;
     }
 }

 private void offlineToggleSwitch_Toggled(object sender, RoutedEventArgs e)
 {
    
     if (offlineToggleSwitch.IsOn == true)
     {
         onlineToggleSwitch.IsOn = false;
         maxToggleSwitch.IsOn = false;
     }
     
 }

 private void maxToggleSwitch_Toggled(object sender, RoutedEventArgs e)
 {
     if (maxToggleSwitch.IsOn == true)
     {
         onlineToggleSwitch.IsOn = false;
         offlineToggleSwitch.IsOn = false;
     }
    
 }
© www.soinside.com 2019 - 2024. All rights reserved.