c# - A set of questions about Set up a VisualStates and TriggerStates for Controls - Stack Overflow

admin2025-04-17  2

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

Share Improve this question edited Feb 5 at 0:09 Andrew KeepCoding 14.2k2 gold badges21 silver badges37 bronze badges asked Jan 31 at 23:57 Eliy ArlevEliy Arlev 5782 gold badges4 silver badges16 bronze badges 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. – Gerry Schmitz 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. – Eliy Arlev 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". – Gerry Schmitz Commented Feb 1 at 19:03
  • Have you checked @Andrew's answer? It might help you. – Roy Li - MSFT Commented Feb 12 at 5:41
  • I wouldn't say it answered my question, but definitely pointed me to a valuable learning resource – Eliy Arlev Commented Feb 22 at 19:35
Add a comment  | 

1 Answer 1

Reset to default 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

转载请注明原文地址:http://anycun.com/QandA/1744843230a88394.html