การผูกหน้าต่างStartupLocation

ฉันกำลังพยายามทำให้หน้าต่างหลักของฉันจดจำและคืนค่าตำแหน่งและขนาดเมื่อเริ่มต้นระบบ ดังนั้นฉันจึงพยายามผูกตำแหน่งเริ่มต้นของหน้าต่างกับคุณสมบัติในโมเดลวิวของฉันดังต่อไปนี้:

<Window x:Class="MyApp.Views.MainWindow"
    ...
    Width="{Binding Width}"
    Height="{Binding Height}"
    WindowStartupLocation="{Binding WindowStartupLocation}"
    WindowState="{Binding WindowState}"
    MinHeight="600"
    MinWidth="800"
    Closing="OnWindowClosing"
    Closed="OnWindowClosed"
    ContentRendered="OnMainWindowReady"
    ...>

มุมมองของฉัน:

        ...

        // Default settings
        WindowState = (WindowState)FormWindowState.Normal;
        this.WindowStartupLocation = WindowStartupLocation.CenterScreen;
        Width = 800;
        Height = 600;

        // check if the saved bounds are nonzero and is visible on any screen
        if (Properties.Settings.Default.WindowStartupLocation != Rectangle.Empty &&
            IsVisibleOnAnyScreen(Properties.Settings.Default.WindowStartupLocation))
        {
            this.WindowStartupLocation = WindowStartupLocation.Manual;
            this.WindowState = (WindowState)Properties.Settings.Default.WindowState;
            Height = Properties.Settings.Default.WindowStartupLocation.Size.Height;
            Width = Properties.Settings.Default.WindowStartupLocation.Size.Width;
            Left = Properties.Settings.Default.WindowStartupLocation.Left;
            Top = Properties.Settings.Default.WindowStartupLocation.Top;
        }
        ...

เมื่อฉันเรียกใช้แอปพลิเคชัน ฉันจะได้รับ System.Windows.Markup.XamlParseException และ ข้อมูลเพิ่มเติม: ไม่สามารถตั้งค่า 'การเชื่อมโยง' บน 'WindowStartupLocation ' คุณสมบัติของประเภท 'MainWindow' สามารถตั้งค่า 'การเชื่อมโยง' ได้เฉพาะใน DependencyProperty ของ DependencyObject เท่านั้น

ฉันจะแก้ไขสิ่งนี้ได้อย่างไร?


person Lynct    schedule 18.12.2014    source แหล่งที่มา


คำตอบ (2)


ลองใช้พฤติกรรมที่แนบมาซึ่งช่วยให้คุณผูกคุณสมบัติ WindowStartupLocation ได้:

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);
            }
        }
    }
}

การใช้งาน:

<Window
  PropertiesExtension:WindowExt.WindowStartupLocation="{Binding ..}" />

ตามที่ระบุไว้ในข้อผิดพลาด WindowStartupLocation ไม่ใช่คุณสมบัติการพึ่งพา ซึ่งหมายความว่าคุณไม่สามารถผูกมันได้ โซลูชันอาจมาจาก Window และการสร้างคุณสมบัติการพึ่งพา หรือใช้ลักษณะการทำงานที่แนบมา

person Erti-Chris Eelmaa    schedule 18.12.2014
comment
อืม ปัญหาดูซับซ้อนกว่านั้นนะ แต่ขอบคุณฉันจะลองดู :) - person Lynct; 19.12.2014

คุณไม่สามารถผูก WindowsStartupLocation ได้ บรรทัดนี้สร้างข้อผิดพลาด:

WindowStartupLocation="{Binding WindowStartupLocation}"

คุณสามารถตั้งค่าเป็นค่าเฉพาะบางอย่างได้ เช่น:

WindowStartupLocation="CenterScreen"
person msporek    schedule 18.12.2014
comment
@Lynct พยายามผูกคุณสมบัติโดยเฉพาะเพื่อรองรับการกู้คืนตำแหน่งเริ่มต้นของหน้าต่าง เขาไม่ต้องการฮาร์ดโค้ดมัน - person NathanAldenSr; 04.08.2016