0

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",

5
  • Why would you not simply "disable" a button if you did not want someone to "press it". All your questions just lead to more questions. Commented Feb 1 at 5:43
  • @GerrySchmitz, I need the button to respond to other UI events like right clicking, tooltip display, etc... for various reasons. The main idea of this question is to have an answer that provides more info about WinUI mechanics covered by the question. Commented Feb 1 at 11:27
  • That's the point: all those things are "common place"; and handled with event handlers; not modifying "visual states"; which IMO, are "make work" projects unrelated to "user requirements". Commented Feb 1 at 19:03
  • Have you checked @Andrew's answer? It might help you. Commented Feb 12 at 5:41
  • I wouldn't say it answered my question, but definitely pointed me to a valuable learning resource Commented Feb 22 at 19:35

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

I wouldn't say it answered my question, but definitely pointed me to a valuable learning resource

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.