在 Windows 窗体中,窗体的属性部分有一个选项,用于在应用程序设置和 Windows 窗体之间建立绑定(bind)。
通常我会得到一个名为 frmMyFormName_Location 的设置,然后根据需要自动更新该设置,我需要做的就是在应用程序退出时调用 Settings.Save() 方法以保留位置。
有人可以在 WPF 中提供相同的示例吗,因为我一直无法弄清楚如何完成此操作?
最佳答案
从 WPF 中的 .settings
文件绑定(bind)到用户或应用程序设置非常简单。
这是一个从设置中获取其位置和大小的窗口示例:
<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();
}
关于c# - WPF 窗口位置绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5372951/