I want to have Border controls to change color when the mouse is hovering over them. I know how to achieve this by subscribing to controls events. However, I have recently discovered the VisualStateManager mechanism and currently confused about when or why should I use it.
It seems that all VisualStates mechanic was meant to allow configuring controls response to user interactions within the XAML. I couldn't set up a VisualState at runtime, only trigger predefined states. Is this assessment correct?
How should I set up VisualStates for controls created programmatically during runtime? Seems I can define a template within a style, and then apply the style to the created control, correct?
VisualStates are somehow connected to Triggers, I assume by name, but there is no property PointerOver, so how come there is a StateTrigger for it?
How should I neutrelize VisualStates of existing controls. For example, I want Buttons not to respond when mouse is hovered over them or if pressed. I would assume again by defining a template within a style and then applying the style, right?
Researched contributed to this point
.ui.xaml.statetrigger?view=windows-app-sdk-1.6
.5-stable/dxaml/xcp/dxaml/themes/generic.xaml
Change text when VisualState is set to "PointerOver",
I want to have Border controls to change color when the mouse is hovering over them. I know how to achieve this by subscribing to controls events. However, I have recently discovered the VisualStateManager mechanism and currently confused about when or why should I use it.
It seems that all VisualStates mechanic was meant to allow configuring controls response to user interactions within the XAML. I couldn't set up a VisualState at runtime, only trigger predefined states. Is this assessment correct?
How should I set up VisualStates for controls created programmatically during runtime? Seems I can define a template within a style, and then apply the style to the created control, correct?
VisualStates are somehow connected to Triggers, I assume by name, but there is no property PointerOver, so how come there is a StateTrigger for it?
How should I neutrelize VisualStates of existing controls. For example, I want Buttons not to respond when mouse is hovered over them or if pressed. I would assume again by defining a template within a style and then applying the style, right?
Researched contributed to this point
https://learn.microsoft.com/en-us/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.statetrigger?view=windows-app-sdk-1.6
https://github.com/microsoft/microsoft-ui-xaml/blob/winui3/release/1.5-stable/dxaml/xcp/dxaml/themes/generic.xaml
Change text when VisualState is set to "PointerOver",
A VisualState
won't be automatically triggered. It has to be triggered by the control:
public void Control_PointerEntered(object sender, PointerRoutedEventArgs e)
{
base.OnPointerEntered(e);
if (VisualStateManager.GoToState(this, "PointerOverState", true) is false)
{
System.Diagnostics.Debug.WriteLine("VisualState 'PointerOverState' failed.");
}
}
Then you can create your VisualStates on XAML. You can see working examples on the CommunityToolkit GitHub repo.
BTW, you can also get a bit tricky and add/remove VisualStates on C#.
https://stackoverflow.com/a/76085521/2411960