2014-12-18 94 views
0

我試圖讓我的主窗口在啓動時記住並恢復位置和大小。所以,我想我的窗口的啓動位置綁定到我的視圖模型一個屬性如下:綁定窗口啓動位置

<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附加信息:無法在「MainWindow」類型的「WindowStartupLocation」屬性上設置「綁定」。 '綁定'只能在DependencyObject的DependencyProperty上設置。

我應該如何糾正?

回答

6

嘗試使用附加的行爲,它可以讓你綁定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派生,並創建依賴項屬性,或使用附加的行爲。

+0

嗯這個問題似乎更復雜,然後我雖然。但謝謝生病給我一個嘗試:) – Lynct 2014-12-18 22:49:44

-2

不能綁定WindowsStartupLocation,此行生成錯誤:

WindowStartupLocation="{Binding WindowStartupLocation}" 

你可以將它設置爲某個特定的值,這樣的:

WindowStartupLocation="CenterScreen" 
+0

@Lynct專門試圖綁定屬性以支持恢復窗口的啓動位置。他不想硬編碼。 – NathanAldenSr 2016-08-04 03:33:01