WPF窗口位置绑定
在Windows窗体中,窗体的属性部分中有一个选项用于在应用程序设置和窗体窗体之间建立绑定。WPF窗口位置绑定
通常我最终会得到一个名为frmMyFormName_Location的设置,然后根据需要自动更新,我需要做的就是调用应用程序退出时的Settings.Save()方法来保存位置。
有人可能请提供一个WPF中的相同的事例,因为我一直无法解决如何实现这个目标?
绑定到WPF中的.settings
文件的用户或应用程序设置非常简单。
下面是会从设置位置和大小的窗口的例子:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:settings="clr-namespace:WpfApplication1.Properties"
Height="{Binding Height, Source={x:Static settings:Settings.Default}, Mode=TwoWay}"
Width="{Binding Width, Source={x:Static settings:Settings.Default}, Mode=TwoWay}"
Top="{Binding Top, Source={x:Static settings:Settings.Default}, Mode=TwoWay}"
Left="{Binding Left, Source={x:Static settings:Settings.Default}, Mode=TwoWay}">
<Grid>
</Grid>
</Window>
的设置是这样的:
而且要坚持,我只是使用以下代码:
void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
Properties.Settings.Default.Save();
}
以下链接可能有助于存储应用程序设置。在WPF窗口中没有称为位置的单个属性,但是您确实有一个LocationChanged事件,您可以相应地处理和编写代码。
粗例子是:
private void Window_LocationChanged(object sender, EventArgs e)
{
var left = (double)GetValue(Window1.LeftProperty);
var top = (double)GetValue(Window1.TopProperty);
// persist these values
. . .
}
对于持续应用程序设置:
c# - approach for saving user settings in a WPF application? 设置-IN-A-WPF的应用程序
虽然我衷心感谢您的意见,但我希望能够通过绑定的方式来声明性地做到这一点。 – 2011-03-21 04:43:15
我努力找到这样一个样本,但不幸的是,大多数人似乎使用代码来实现这一点。看看这个: http://www.codeproject.com/KB/WPF/SaveRestoreWPFWindowSize.aspx – 2011-03-21 05:26:56
这里是WPF的一个例子VB.NET
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication1"
xmlns:Properties="clr-namespace:WpfApplication1"
Title="Test"
Loaded="Window_Loaded" Closing="Window_Closing"
Height="{Binding Height, Source={x:Static Properties:MySettings.Default}, Mode=TwoWay}"
Width="{Binding Width,Source={x:Static Properties:MySettings.Default}, Mode=TwoWay}"
Left="{Binding Left,Source={x:Static Properties:MySettings.Default}, Mode=TwoWay}"
Top="{Binding Top, Source={x:Static Properties:MySettings.Default}, Mode=TwoWay}"
>
<Grid Name="MainFormGrid"> ...
你是枪!谢谢... – 2011-03-22 00:57:05
madd0 - 你摇滚。真棒回复,感谢 – 2013-09-25 16:19:23
+1模式= TwoWay' – Daryn 2014-05-09 17:17:26