C# 绑定窗口起始位置
我试图让我的主窗口在启动时记住并恢复位置和大小。因此,我尝试将窗口的启动位置绑定到viewmodel中的属性,如下所示:C# 绑定窗口起始位置,c#,wpf,xaml,mvvm,C#,Wpf,Xaml,Mvvm,我试图让我的主窗口在启动时记住并恢复位置和大小。因此,我尝试将窗口的启动位置绑定到viewmodel中的属性,如下所示: <Window x:Class="MyApp.Views.MainWindow" ... Width="{Binding Width}" Height="{Binding Height}" WindowStartupLocation="{Binding WindowStartupLocation}" WindowState="{B
<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"
...>
运行应用程序时,我会得到一个System.Windows.Markup.XamlParseException和附加信息:无法在类型为“MainWindow”的“WindowsStartUpLocation”属性上设置“Binding”。“绑定”只能在DependencyObject的DependencyProperty上设置。
如何更正此错误?您无法绑定WindowsStartupLocation,此行将生成错误:
WindowStartupLocation="{Binding WindowStartupLocation}"
您可以将其设置为某个特定值,如下所示:
WindowStartupLocation="CenterScreen"
无法绑定WindowsStartupLocation,此行将生成错误:
WindowStartupLocation="{Binding WindowStartupLocation}"
您可以将其设置为某个特定值,如下所示:
WindowStartupLocation="CenterScreen"
尝试使用附加行为,该行为允许您绑定
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
不是依赖属性,这意味着您无法绑定它。解决方案可以从窗口派生,并创建依赖项属性,也可以使用附加行为。尝试使用附加行为,该行为允许您绑定窗口起始位置
属性:
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
不是依赖属性,这意味着您无法绑定它。解决方案可以是从窗口
派生,创建依赖属性,或者使用附加行为。Hmm问题似乎比我想象的更复杂。但是谢谢,我试试看:)嗯,这个问题似乎比我想的更复杂。但是谢谢,我会尝试一下:)@Lync特别尝试绑定属性以支持恢复窗口的启动位置。他不想硬编码。@Lync特别尝试绑定该属性以支持还原窗口的启动位置。他不想硬编码。