到WPF属性绑定到在C#中的applicationSettings的最好方法? [英] Best way to bind WPF properties to ApplicationSettings in C#?

查看:474
本文介绍了到WPF属性绑定到在C#中的applicationSettings的最好方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是WPF属性绑定到在C#中的applicationSettings最好的方法是什么?有没有像在Windows窗体应用程序自动方式?类似<一个href="http://stackoverflow.com/questions/18421/best-way-to-bind-windows-forms-properties-to-applicationsettings-in-c">this问题,如何(以及是否有可能),你会怎么做同样的事情在WPF?

What is the best way to bind WPF properties to ApplicationSettings in C#? Is there an automatic way like in a Windows Forms Application? Similar to this question, how (and is it possible to) do you do the same thing in WPF?

推荐答案

您可以直接绑定到Visual Studio创建静态对象。

You can directly bind to the static object created by Visual Studio.

在你的窗户声明添加:

xmlns:p="clr-namespace:UserSettings.Properties"

其中, UserSettings 是应用程序的命名空间。

where UserSettings is the application namespace.

然后你可以添加绑定到正确的设置:

Then you can add a binding to the correct setting:

<TextBlock Height="{Binding Source={x:Static p:Settings.Default}, 
           Path=Height, Mode=TwoWay}" ....... />

现在,你可以当你关闭你的应用程序保存设置,每例如:

Now you can save the settings, per example when you close your application:

protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
{
    Properties.Settings.Default.Save();
    base.OnClosing(e); 
}

这篇关于到WPF属性绑定到在C#中的applicationSettings的最好方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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