Reputation: 289
For reasons not relevant here I need to set VisualState in C# rather than XAML. However, I don't know how to change VisualState in response to a CollectionView's Selection change.
Here's the working XAML code which I'm trying to replicate in C#:
<CollectionView SelectionMode="Multiple"
ItemsSource="{Binding ViewModel_Contacts}"
SelectionChanged="OnSelectionChanged">
<CollectionView.ItemTemplate>
<DataTemplate x:DataType="sharedModels:Showuser">
<StackLayout Orientation="Horizontal" HeightRequest="40">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal">
<VisualState.Setters>
<Setter TargetName="LabelMark" Property="Label.TextColor"
Value="Blue"/>
</VisualState.Setters>
</VisualState>
<VisualState x:Name="Selected">
<VisualState.Setters>
<Setter TargetName="LabelMark" Property="Label.TextColor"
Value="Red"/>
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Label x:Name="LabelMark"/>
</StackLayout>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
The way I have VisualState set in C# is as follows:
VisualStateManager.GetVisualStateGroups(LabelMark).Add(
new VisualStateGroup() {
Name= "CommonStates",
States = {
new VisualState {
Name = "Normal",
Setters = {
new Setter {
Property = Label.TextColorProperty,
Value = Colors.Blue
}
},
StateTriggers= {
new StateTrigger {
/* THIS IS THE QUESTION:
HOW DO I SET IT UP? */
}
}
},
new VisualState {
Name = "Selected",
Setters = {
new Setter {
Property = Label.TextColorProperty,
Value = Colors.Red
}
},
StateTriggers= {
new StateTrigger {
/* THIS IS THE QUESTION:
HOW DO I SET IT UP? */
}
}
}
}
}
);
I don't know how to set up trigger(s) for the above scenario. It seems obvious that the trigger should be bound to the containing CollectionView's selection changed event, but I don't know how to get to it.
Upvotes: 3
Views: 1651
Reputation: 162
Looking at the Microsoft docs for StateTrigger and there is only one property that you can set. It's IsActive
and indicates whether a trigger should be applied. So StateTrigger
would look like this.
new StateTrigger {
IsActive= true
}
If you are looking to bound your VisualState
to a specific target, you should change your Setters
. From the Microsoft docs for Setter Class. There is a property called Target
so that you can target that element and apply the Value
to it.
It looks like this what done on the XAML side as well, but with the property TargetName
which does not exist.
Hope this helps.
Sorry I did look at the wrong Microsoft Docs. I went back and looked at the correct Docs as well as some examples from GitHub. Unfortunately the StateTrigger
was only referenced in the XAML code.
This is the code that I was looking at if you are interested. XAML ViewModel
That being said I think I managed to work out how its done from trying it out in Visual Studio. You can connect the StateTrigger
via a BindingContext
to your ViewModel
. In your case I'm pretty sure in your case it would be this.
new StateTrigger {
BindingContext = new ViewModel_Contacts()
}
Hope that helps for real this time.
Upvotes: 1