WPF窗口位置绑定 [英] WPF window location binding

查看:821
本文介绍了WPF窗口位置绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Windows窗体有一个表格的属性部分的选项建立一个应用程序设置和Windows窗体之间的绑定。

In windows forms there was an option in the properties section of a form to establish a binding between an application setting and the windows form.

通常情况下我最终会得到一个名为frmMyFormName_Location这是必需的,然后自动地更新设置和所有我需要做的是调用应用程序退出的Settings.Save()方法来坚持的位置。

Typically I would end up with a setting called frmMyFormName_Location this was then automagically updated as required and all I needed to do was call the Settings.Save() method on application exit to persist location.

可能有人请提供WPF中同样的事情的例子,因为我一直无法工作,如何做到这一点?

Could someone please provide an example of the same thing in WPF as I have been unable to work out how to accomplish this?

推荐答案

这是非常简单的在WPF中的 .settings 文件绑定到用户或应用程序设置。

It's extremely simple to bind to user or application settings from a .settings file in WPF.

下面是获取其位置和大小从设置窗口的例子:

Here's an example of a window that gets its position and size from 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>

的设置是这样的:

The settings look like this:

和坚持,我只是用下面的code:

And to persist, I'm simply using the following code:

void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
    Properties.Settings.Default.Save();
}

这篇关于WPF窗口位置绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆