Page 1 of 1

Is there a way to debug the current visual state?

Posted: 17 Aug 2022, 01:42
by samc
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

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

Posted: 17 Aug 2022, 10:52
by sfernandez
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