Reputation: 539
I am trying to use a ToggleSwitch with MVVM Light in a Wp7 app.
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" >
The 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 trap the "state changed" event; Checked and Unchecked. If I bind these with something like: Checked="{Binding Path=AccuracyChanged}" I get a runtime error telling me the markup is wrong.
I also unsuccessfully tried a 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 work just fine 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 a ToggleSwitch looks better (IMHO)
Any suggestions appreciated.
Upvotes: 4
Views: 2282
Reputation: 1215
I believe the problem with your markup may be that the IsChecked
Property of the ToggleButton can be three states; null
,true
orfalse
.You are binding it to a boolean but might want to try making it a System.Nullable<Boolean>
(bool?
) to allow for the three states. I believe this is still necessary even if you set IsThreeState = false
;
Upvotes: 5