private static void SetAyVisualStateGroups(DependencyObject d, AyVisualStateGroupCollection value)
        {
            d.SetValue(AyVisualStateGroupsPropertyKey, value);
            INotifyCollectionChanged collection = (INotifyCollectionChanged)value;

            collection.CollectionChanged += new NotifyCollectionChangedEventHandler(CollectionChanged);
        }
 public static AyVisualStateGroupCollection GetAyVisualStateGroups(DependencyObject d)
 {
     if (d == null)
     {
         throw new InvalidOperationException("The dependency object trying to attach to is set to null");
     }
     if (!WpfTreeHelper.IsInDesignMode)
     {
         AyVisualStateGroupCollection collection = d.GetValue(AyVisualStateManager.AyVisualStateGroupsProperty) as AyVisualStateGroupCollection;
         if (collection == null)
         {
             collection       = new AyVisualStateGroupCollection();
             collection.Owner = d;
             var _2 = d as FrameworkElement;
             if (_2 != null)
             {
                 Microsoft.Win32.SystemEvents.DisplaySettingsChanged += (sen, eargs) =>
                 {
                     if (System.Windows.SystemParameters.PrimaryScreenWidth > System.Windows.SystemParameters.PrimaryScreenHeight)
                     {
                         ScreenDirection = AyOrientation.Horizontal;
                     }
                     else
                     {
                         ScreenDirection = AyOrientation.Vertical;
                     }
                     WhenOwnerSizeChanged(new Size(_2.ActualWidth, _2.ActualHeight), collection, _2);
                 };
                 if (System.Windows.SystemParameters.PrimaryScreenWidth > System.Windows.SystemParameters.PrimaryScreenHeight)
                 {
                     ScreenDirection = AyOrientation.Horizontal;
                 }
                 else
                 {
                     ScreenDirection = AyOrientation.Vertical;
                 }
                 _2.SizeChanged += (sender, args) =>
                 {
                     var _nowSize = args.NewSize;
                     WhenOwnerSizeChanged(_nowSize, collection, _2);
                 };
             }
             SetAyVisualStateGroups(d, collection);
         }
         return(collection);
     }
     return(new AyVisualStateGroupCollection());
 }
        private static void WhenOwnerSizeChanged(Size _nowSize, AyVisualStateGroupCollection collection, FrameworkElement _2)
        {
            var _31 = collection.FirstOrDefault(x => x.Orientation == AyOrientation.Both);

            if (_31 != null)
            {
                //执行
                var _4 = _31.VisualStates;
                foreach (var item in _4)
                {
                    if (item.MinWindowWidth.HasValue && !item.MinWindowHeight.HasValue)
                    {
                        if (_nowSize.Width <= item.MinWindowWidth.Value)
                        {
                            ExecuteSetters(item, _2);
                            if (item.SetterMode == VisualStateSetteMode.Continue)
                            {
                                continue;
                            }
                            else
                            {
                                break;
                            }
                        }
                    }
                    else if (!item.MinWindowWidth.HasValue && item.MinWindowHeight.HasValue)
                    {
                        if (_nowSize.Height <= item.MinWindowHeight.Value)
                        {
                            ExecuteSetters(item, _2);
                            if (item.SetterMode == VisualStateSetteMode.Continue)
                            {
                                continue;
                            }
                            else
                            {
                                break;
                            }
                        }
                    }
                    else if (item.MinWindowWidth.HasValue && item.MinWindowHeight.HasValue)
                    {
                        if (_nowSize.Height <= item.MinWindowHeight.Value && _nowSize.Width <= item.MinWindowWidth.Value)
                        {
                            ExecuteSetters(item, _2);
                            if (item.SetterMode == VisualStateSetteMode.Continue)
                            {
                                continue;
                            }
                            else
                            {
                                break;
                            }
                        }
                    }

                    else
                    if (item.MaxWindowWidth.HasValue && !item.MaxWindowHeight.HasValue)
                    {
                        if (_nowSize.Width > item.MaxWindowWidth.Value)
                        {
                            ExecuteSetters(item, _2);
                            if (item.SetterMode == VisualStateSetteMode.Continue)
                            {
                                continue;
                            }
                            else
                            {
                                break;
                            }
                        }
                    }
                    else if (!item.MaxWindowWidth.HasValue && item.MaxWindowHeight.HasValue)
                    {
                        if (_nowSize.Height > item.MaxWindowHeight.Value)
                        {
                            ExecuteSetters(item, _2);
                            if (item.SetterMode == VisualStateSetteMode.Continue)
                            {
                                continue;
                            }
                            else
                            {
                                break;
                            }
                        }
                    }
                    else if (item.MaxWindowWidth.HasValue && item.MaxWindowHeight.HasValue)
                    {
                        if (_nowSize.Height > item.MaxWindowHeight.Value && _nowSize.Width > item.MaxWindowWidth.Value)
                        {
                            ExecuteSetters(item, _2);
                            if (item.SetterMode == VisualStateSetteMode.Continue)
                            {
                                continue;
                            }
                            else
                            {
                                break;
                            }
                        }
                    }
                }
            }

            var _3 = collection.FirstOrDefault(x => x.Orientation == ScreenDirection);

            if (_3 != null)
            {
                var _4 = _3.VisualStates;
                foreach (var item in _4)
                {
                    if (item.MinWindowWidth.HasValue && !item.MinWindowHeight.HasValue)
                    {
                        if (_nowSize.Width <= item.MinWindowWidth.Value)
                        {
                            ExecuteSetters(item, _2);
                            if (item.SetterMode == VisualStateSetteMode.Continue)
                            {
                                continue;
                            }
                            else
                            {
                                break;
                            }
                        }
                    }
                    else if (!item.MinWindowWidth.HasValue && item.MinWindowHeight.HasValue)
                    {
                        if (_nowSize.Height <= item.MinWindowHeight.Value)
                        {
                            ExecuteSetters(item, _2);
                            if (item.SetterMode == VisualStateSetteMode.Continue)
                            {
                                continue;
                            }
                            else
                            {
                                break;
                            }
                        }
                    }
                    else if (item.MinWindowWidth.HasValue && item.MinWindowHeight.HasValue)
                    {
                        if (_nowSize.Height <= item.MinWindowHeight.Value && _nowSize.Width <= item.MinWindowWidth.Value)
                        {
                            ExecuteSetters(item, _2);
                            if (item.SetterMode == VisualStateSetteMode.Continue)
                            {
                                continue;
                            }
                            else
                            {
                                break;
                            }
                        }
                    }

                    else
                    if (item.MaxWindowWidth.HasValue && !item.MaxWindowHeight.HasValue)
                    {
                        if (_nowSize.Width > item.MaxWindowWidth.Value)
                        {
                            ExecuteSetters(item, _2);
                            if (item.SetterMode == VisualStateSetteMode.Continue)
                            {
                                continue;
                            }
                            else
                            {
                                break;
                            }
                        }
                    }
                    else if (!item.MaxWindowWidth.HasValue && item.MaxWindowHeight.HasValue)
                    {
                        if (_nowSize.Height > item.MaxWindowHeight.Value)
                        {
                            ExecuteSetters(item, _2);
                            if (item.SetterMode == VisualStateSetteMode.Continue)
                            {
                                continue;
                            }
                            else
                            {
                                break;
                            }
                        }
                    }
                    else if (item.MaxWindowWidth.HasValue && item.MaxWindowHeight.HasValue)
                    {
                        if (_nowSize.Height > item.MaxWindowHeight.Value && _nowSize.Width > item.MaxWindowWidth.Value)
                        {
                            ExecuteSetters(item, _2);
                            if (item.SetterMode == VisualStateSetteMode.Continue)
                            {
                                continue;
                            }
                            else
                            {
                                break;
                            }
                        }
                    }
                }
            }
        }
        static void CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            AyVisualStateGroupCollection sourceCollection = (AyVisualStateGroupCollection)sender;

            switch (e.Action)
            {
            case NotifyCollectionChangedAction.Add:
                if (e.NewItems != null)
                {
                    foreach (AyVisualStateGroup item in e.NewItems)
                    {
                        item.Owner = sourceCollection.Owner;
                    }
                }
                break;

            case NotifyCollectionChangedAction.Remove:
                if (e.OldItems != null)
                {
                    foreach (AyVisualStateGroup item in e.OldItems)
                    {
                        //item.Behavior.Dispose();
                    }
                }
                break;


            case NotifyCollectionChangedAction.Replace:
                if (e.NewItems != null)
                {
                    foreach (AyVisualStateGroup item in e.NewItems)
                    {
                        item.Owner = sourceCollection.Owner;
                    }
                }

                if (e.OldItems != null)
                {
                    foreach (AyVisualStateGroup item in e.OldItems)
                    {
                        //item.Behavior.Dispose();
                    }
                }
                break;

            //when an item(s) is removed we should Dispose the BehaviorBinding
            case NotifyCollectionChangedAction.Reset:
                if (e.OldItems != null)
                {
                    foreach (AyVisualStateGroup item in e.OldItems)
                    {
                        //item.Behavior.Dispose();
                    }
                }
                break;

            case NotifyCollectionChangedAction.Move:
            default:
                break;
            }
        }