Setting WindowStartupLocation from ResourceDictionary throws XamlParseException

前端 未结 2 1271
误落风尘
误落风尘 2020-12-20 15:47

When I attempt to set the WindowStartupLocation property through a Setter within a ResourceDictionary, I get a XamlParseExceptio

2条回答
  •  一个人的身影
    2020-12-20 16:12

    WindowStartupLocation is a CLR property, this can be seen in ILSpy:

    [DefaultValue(WindowStartupLocation.Manual)]
    public WindowStartupLocation WindowStartupLocation
    {
        get
        {
            this.VerifyContextAndObjectState();
            this.VerifyApiSupported();
            return this._windowStartupLocation;
        }
    
        set
        {
            this.VerifyContextAndObjectState();
            this.VerifyApiSupported();
    
            if (!Window.IsValidWindowStartupLocation(value))
            {
                throw new InvalidEnumArgumentException("value", (int)value, typeof(WindowStartupLocation));
            }
    
            this._windowStartupLocation = value;
        }
    }
    

    In the style setters can only be specified dependency property. There are two ways to solve this problem:

    • inherit class Window, and create your class with a dependency property WindowStartupLocation

    • create an attached property type depending WindowStartupLocation and define the logic in the PropertyChanged

    The first method is cumbersome, because it is necessary to redefine the class for one property. The second method is preferred, and will be attached behavior, but I'll call PropertyExtension.

    Here is the complete code:

    namespace YourProject.PropertiesExtension
    {
        public static class WindowExt
        {
            public static readonly DependencyProperty WindowStartupLocationProperty;
    
            public static void SetWindowStartupLocation(DependencyObject DepObject, WindowStartupLocation value)
            {
                DepObject.SetValue(WindowStartupLocationProperty, value);
            }
    
            public static WindowStartupLocation GetWindowStartupLocation(DependencyObject DepObject)
            {
                return (WindowStartupLocation)DepObject.GetValue(WindowStartupLocationProperty);
            }
    
            static WindowExt() 
            {            
                WindowStartupLocationProperty = DependencyProperty.RegisterAttached("WindowStartupLocation",
                                                          typeof(WindowStartupLocation),
                                                          typeof(WindowExt),
                                                          new UIPropertyMetadata(WindowStartupLocation.Manual, OnWindowStartupLocationChanged));
            }
    
            private static void OnWindowStartupLocationChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
            {
                Window window = sender as Window; 
    
                if (window != null) 
                {
                    window.WindowStartupLocation = GetWindowStartupLocation(window);
                }
            }
        }
    }
    

    Example of using:

    
    
        
    
    

提交回复
热议问题