简体   繁体   English

清除WPF中的“设置”绑定

[英]Clean “settings” binding in WPF

I was recently working with a lot of bindings to my configuration settings in XAML. 我最近在XAML中对我的配置设置进行了大量绑定。 Storing column widths/control sizes/window positions and the like. 存储列宽/控制尺寸/窗口位置等。 So I was wondering if there was an easy way to create and bind to "settings/configuration" values to XAML? 所以我想知道是否有一种简单的方法来创建并绑定到XAML的“设置/配置”值?

Right now I just create a setting in the project, shove a bindable property into the XAML's DataContext and go from there. 现在我只是在项目中创建一个设置,将一个可绑定的属性推送到XAML的DataContext中并从那里开始。 But my settings count is getting pretty crazy and managing them is getting painful (boring, repetitive, and annoying). 但我的设置数量变得非常疯狂,管理它们变得很痛苦(无聊,重复和烦人)。

In an ideal world I'd like a system where I can do something like this: 在一个理想的世界里,我想要一个我可以做这样的事情的系统:

<Window State={Binding {Settings Name="MyWindowState", DefaultValue="Normal"}}/>

If the "MyWindowState" setting doesn't exist, it would be created automatically and stored somewhere. 如果“MyWindowState”设置不存在,它将自动创建并存储在某处。 And if the MyWindowState setting changes, all the bindings that use it would also be notified and updated accordingly. 如果MyWindowState设置发生更改,则使用它的所有绑定也会相应地得到通知和更新。 And the DefaultValue would be used if the setting retrieval failed. 如果设置检索失败,将使用DefaultValue。

Does something akin to this exist already, or can it be achieved with the standard WPF XAML? 是否存在类似于此的东西,或者是否可以使用标准WPF XAML实现?

I am planning on working on something that can do this, but if a proven solution already exists I would love to at least look at it/hear it out. 我正在计划开展一些可以做到这一点的事情,但是如果一个经过验证的解决方案已经存在,我很乐意至少看一下/听出来。

From what I understand Telerik's persistance framework can do something like this, except on a control to control basis (there is no global "settings" I can bind to), at least on first glance. 根据我的理解, Telerik的持久性框架可以做这样的事情,除了控制基础控制(没有全局“设置”我可以绑定),至少乍一看。

You can do this with an attached property: 您可以使用附加属性执行此操作:

<Window loc:WindowState.Name="MyWindowState" />

In the OnNameChanged event handler of your attached property you will have access to the Window instance that the WindowState.Name property was set on and access to the value ( "MyWindowState" in this example) that was set. 在附加属性的OnNameChanged事件处理程序中,您将可以访问设置了WindowState.Name属性的Window实例,并可以访问已设置的值(在此示例中为“MyWindowState” )。 There you start listening (eg using PropertyChangedEventManager ) to changes of all properties of your Window instance that are part of your window state that you want to persist. 在那里,您开始监听(例如,使用PropertyChangedEventManager )更改Window实例的所有属性,这些属性是您要保留的窗口状态的一部分。

May be you can use WPF's theming option. 也许你可以使用WPF的主题选项。 You can store settings of your controls (width, color...) in a theme file, which is nothing but an xml file. 您可以将控件的设置(宽度,颜色...)存储在主题文件中,该文件只是一个xml文件。 You may store this xml somewhere and load it at runtime. 您可以将此xml存储在某处并在运行时加载它。 You can update this xml when the application exits with the changes. 当应用程序退出时,您可以更新此xml。 And load it when the application opens next. 并在下一次打开应用程序时加载它。

Yes, it's quite possible. 是的,这很有可能。 If you have an application properties file, you can access it like this: 如果您有应用程序属性文件,则可以像这样访问它:

Height="{Binding MainWindowHeight, Mode=TwoWay, Source={x:Static p:Settings.Default}}"

where MainWindowHeight is a setting (in my case, an int). MainWindowHeight是一个设置(在我的例子中,是一个int)。 You'll also need to include this in the top of your XAML file, in the Window or UserControl tag: 您还需要将它包含在XAML文件的顶部,在WindowUserControl标记中:

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

where APPLICATION_NAME is the name of your application. 其中APPLICATION_NAME是您的应用程序的名称。

EDIT: The binding can be have any mode, I just use TwoWay so I don't have to have any actual code to update it. 编辑:绑定可以有任何模式,我只是使用TwoWay所以我没有任何实际的代码来更新它。 For the positioning of my windows, it works out nicely that way. 对于我的窗户的定位,这样做很好。

EDIT: Also, this can't dynamically create settings. 编辑:此外,这不能动态创建设置。 I would use an XML file in your application, make a class to handle that, and then bind to a method of the class to get/dynamically create the values. 我将在您的应用程序中使用XML文件,创建一个类来处理它,然后绑定到类的方法以获取/动态创建值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM