WPF 自定义附加属性

 public static bool GetChangedSatesProperty(DependencyObject obj)
        {
            return (bool)obj.GetValue(ChangedSatesProperty);
        }

        public static void SetChangedSatesProperty(DependencyObject obj, bool value)
        {
            obj.SetValue(ChangedSatesProperty, value);
        }

        public static readonly DependencyProperty ChangedSatesProperty =
            DependencyProperty.RegisterAttached(
                "ChangedSatesProperty",
                typeof(bool),
                typeof(StateManager),
                new PropertyMetadata((dependencyObject, args) =>
                {
                    var frameworkElement = dependencyObject as FrameworkElement;
                    if (frameworkElement == null && !(bool)args.NewValue)
                        return;
                    if (frameworkElement != null)
                    {
                        
                        frameworkElement .MouseLeftButtonDown += new MouseButtonEventHandler(ChangedSates_MouseLeftButtonDown);
                    }
                }));

        static void ChangedSates_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if ((sender as DependencyObject) == null||e.ClickCount!=2) return;
            var ancestor = DependencyObjectExtensions.FindAncestor(sender as DependencyObject);
            if (ancestor != null)
                ancestor.WindowState = ancestor.WindowState == WindowState.Normal
                                           ? WindowState.Maximized
                                           : WindowState.Normal;
        }
可以在 frameworkElement .MouseLeftButtonDown += new MouseButtonEventHandler(ChangedSates_MouseLeftButtonDown);处添加任何 对frameworkElement 的操作。比如缓动动画 所有子元素 load 增加 动画。或者 unload的时候增加事件处理。在这里记录下 已被后用

你可能感兴趣的:(WPF,相关)