System.Windows.Controls.VisualStates.TryGetVisualStateGroup C# (CSharp) Method

TryGetVisualStateGroup() public static method

This method tries to get the named VisualStateGroup for the dependency object. The provided object's ImplementationRoot will be looked up in this call.
public static TryGetVisualStateGroup ( DependencyObject dependencyObject, string groupName ) : VisualStateGroup
dependencyObject DependencyObject The dependency object.
groupName string The visual state group's name.
return VisualStateGroup
        public static VisualStateGroup TryGetVisualStateGroup(DependencyObject dependencyObject, string groupName)
        {
            FrameworkElement root = GetImplementationRoot(dependencyObject);
            if(root == null)
            {
                return null;
            }

// ReSharper disable ReplaceWithSingleCallToFirstOrDefault
// ReSharper disable AssignNullToNotNullAttribute
            return VisualStateManager.GetVisualStateGroups(root)
// ReSharper restore AssignNullToNotNullAttribute
                .OfType<VisualStateGroup>()
                .Where(group => string.CompareOrdinal(groupName, group.Name) == 0)
                .FirstOrDefault();
// ReSharper restore ReplaceWithSingleCallToFirstOrDefault
        }
    }

Usage Example

示例#1
0
        /// <summary>
        /// Should be called by the parent control before the base
        /// OnApplyTemplate method is called.
        /// </summary>
        public void BeforeOnApplyTemplate()
        {
            if (UsesClosingVisualState)
            {
                // Unhook the event handler for the popup closed visual state group.
                // This code is used to enable visual state transitions before
                // actually setting the underlying Popup.IsOpen property to false.
                VisualStateGroup groupPopupClosed = VisualStates.TryGetVisualStateGroup(Parent, VisualStates.GroupPopup);
                if (null != groupPopupClosed)
                {
                    groupPopupClosed.CurrentStateChanged -= OnPopupClosedStateChanged;
                    UsesClosingVisualState = false;
                }
            }

            if (Popup != null)
            {
                Popup.Closed -= Popup_Closed;
            }
        }
All Usage Examples Of System.Windows.Controls.VisualStates::TryGetVisualStateGroup