samc
Topic Author
Posts: 48
Joined: 21 Aug 2019, 19:22

Is there a way to debug the current visual state?

17 Aug 2022, 01:42

I'm finding that often times I have controls with multiple visual state groups, and it isn't always clear to me what the current visual state of each group is. Is there a way to debug this / get this? I was trying to look for something in the noesis inspector tool but I couldn't figure it out.

thanks!
sam
 
User avatar
sfernandez
Site Admin
Posts: 2710
Joined: 22 Dec 2011, 19:20

Re: Is there a way to debug the current visual state?

17 Aug 2022, 10:52

There is no such information in the Inspector, but in code you can get the collection of visual state groups and ask for the current state:
void PrintCurrentStates(Control control)
{
  FrameworkElement root = VisualTreeHelper.GetChild(control, 0) as FrameworkElement;
  VisualStateGroupCollection groups = VisualStateManager.GetVisualStateGroups(root);
  foreach (VisualStateGroup group in groups)
  {
    VisualState state = group.GetCurrentState(root);
    Console.WriteLine($"Group: {group.Name}, CurrentState: {state.Name}");
  }
}
In a Control visual states are defined in the template root, for UserControls they are defined in the Content, so you have to get the groups collection from the Content element.

We will study how to expose this information in the Inspector to improve debugging in those situations, I created a ticket for that: #2399

Who is online

Users browsing this forum: Ahrefs [Bot], Semrush [Bot] and 3 guests