In a UWP application with MVVM Light I'm trying to bind the current state of a VisualStateGroup
to a property in the ViewModel. The idea is to change the current state by changing the value of a property in the view model according to the application logic.
This problem has been already addressed in this question several years ago. I'm interested in the approach suggested by Olav (second answer), which cannot be directly followed because my application is a UWP app.
Here comes my xaml:
<Page
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:Interactivity="using:Microsoft.Xaml.Interactivity"
xmlns:Core="using:Microsoft.Xaml.Interactions.Core"
x:Class="Smallrobots.Ev3RemoteController.Views.Settings"
mc:Ignorable="d"
DataContext="{Binding MainViewModel, Source={StaticResource Locator}}">
<Grid>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="ConnectionStatus">
<Interactivity:Interaction.Behaviors>
<Core:DataTriggerBehavior Binding="{Binding VsmConnectionStatus}"
Value="{Binding VsmConnectionStatus}">
<Core:GoToStateAction StateName="{Binding VsmConnectionStatus}"/>
</Core:DataTriggerBehavior>
</Interactivity:Interaction.Behaviors>
<VisualState x:Name="Connected">
<VisualState.Setters>
<Setter Target="button.(Control.IsEnabled)" Value="False"/>
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Button x:Name="button" Content="Connect"/>
</Grid>
</Page>
and the MainViewModel
:
public class MainViewModel : ViewModelBase
{
private string vsmConnectionStatus = "";
public string VsmConnectionStatus
{
get => vsmConnectionStatus;
set
{
if (!vsmConnectionStatus.Equals(value))
{
vsmConnectionStatus = value;
RaisePropertyChanged("VsmConnectionStatus");
}
}
}
public MainViewModel()
{
VsmConnectionStatus = "Connected";
}
}
However with this code, after the constructor has been executed, the current state remains null
and further assignments have no effect. It could be the use of the DataTriggerBehavior
or of the GoToStateAction
.
What is wrong? Many thanks!
EDIT: I added some more code to the xaml, to better clarify the question.