2014-12-18 95 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