How to use ToggleSwitch with MVVM Light in WP7?

I am trying to use ToggleSwitch with MVVM Light in a Wp7 application.

In the view, I have:

<toolkit:ToggleSwitch x:Name="toggleAccuracy" Content="{Binding AccuracyText, Mode=TwoWay}" IsChecked="{Binding AccuracyHigh, Mode=TwoWay}" IsEnabled="True" Header="Accuracy" Margin="8,0" Height="140" VerticalAlignment="Top" > 

ViewModel contains:

 private bool _AccuracyHigh = true; public bool AccuracyHigh { get { return _AccuracyHigh; } set { _AccuracyHigh = value; } } private string _AccuracyText = "High"; public string AccuracyText { get { return _AccuracyText; } set { _AccuracyText = value; } } 

AccuracyText and AccuracyHigh fire to get the appropriate values.

My difficulty is trying to capture the "state changed" event; Checked and removed. If I associate them with something like: Checked = "{Binding Path = AccuracyChanged}" I get a runtime error indicating that the markup is incorrect.

I also tried unsuccessfully to trigger the trigger:

  <i:Interaction.Triggers> <i:EventTrigger EventName="Checked"> <GalaSoft_MvvmLight_Command:EventToCommand Command="{Binding AccuracyChanged}"/> </i:EventTrigger> </i:Interaction.Triggers> </toolkit:ToggleSwitch> 

I could use RadioButton, which works great with

 <RadioButton Content="Up" x:Name="radioButton1" GroupName="UpDown" Width="119" /> <RadioButton IsChecked="{Binding UpDown, Mode=TwoWay}" Content="Down" x:Name="radioButton2" GroupName="UpDown" /> public bool UpDown { get { bool ud = settings.UpDown.ToLower() == "u" ? true : false; return ud; } set { settings.UpDown = value == true ? "U" : "D"; settings.Save(); } } 

but ToggleSwitch looks better (IMHO)

Any suggestions appreciated.

+4
source share
1 answer

I believe that the problem with your markup might be that the IsChecked ToggleButton Property can have three states; null , true or false . You associate it with a boolean, but you can try to make it System.Nullable<Boolean> ( bool? ) To allow three states. I believe this is still necessary, even if you set IsThreeState = false ;

+5
source

Source: https://habr.com/ru/post/1347734/


All Articles