Silverlight: VisualStateManager.GetVisualStateGroups doesn't, How can I get them?

假如想象 提交于 2019-12-24 01:51:34

问题


I've tried using VisualStateManager.GetVisualStateGroups in the OnAttached override of my custom behavior, as well as in an event handler added to AssociatedObject.Loaded event in that behavior. Both times I get an empty list.

Is there another way to get the visual state groups defined for a control, or another event handler I should attach to?

Be for it's asked, yes, the control has VisualStateGroups and VisualStates.


回答1:


Usually the VisualStateGroups attached property is attached to the top level FrameworkElement in the control's ControlTemplate. Hence to retrieve this value you may need to use the VisualTreeHelper to get the first child of the control and see if that has a VisualStateGroups property.




回答2:


Based on Anthony's answer. Here I give an example for Metro App.

public VisualState GetCurrentState(string stateGroupName)
{
    VisualStateGroup stateGroup1 = null;

    IList<VisualStateGroup> list = VisualStateManager.GetVisualStateGroups(VisualTreeHelper.GetChild(this, 0) as FrameworkElement);

    foreach (var v in list)
        if (v.Name == stateGroupName)
        {
            stateGroup1 = v;
            break;
        }

    return stateGroup1.CurrentState;
}


来源:https://stackoverflow.com/questions/6753279/silverlight-visualstatemanager-getvisualstategroups-doesnt-how-can-i-get-them

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!