绑定到设置中定义的值
在 WPF 中,我可以使用与“设置”中定义的值进行绑定吗? 如果可能的话,请提供样品。
In WPF, Can I use binding with values defined in Settings? If this is possible, please provide a sample.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
首先,您需要添加一个自定义 XML 命名空间,该命名空间将设计定义设置的命名空间:
然后,在 XAML 文件中,使用以下语法访问默认设置实例:
因此,这是最终结果代码:
来源: WPF - 如何将控件绑定到设置中定义的属性?
注意:正如 @Daniel 和 @nabulke 所指出的,不要忘记设置 设置文件的访问修饰符为
Public
,范围为User
First, you need to add a custom XML namespace that will design the namespace where the settings are defined:
Then, in your XAML file, access the default settings instance using the following syntax:
So here is the final result code:
Source: WPF - How to bind a control to a property defined in the Settings?
Note: As pointed out by @Daniel and @nabulke, don't forget to set Access Modifier of your settings file to
Public
and Scope toUser
上面的解决方案确实有效,但我发现它非常冗长...您可以使用自定义标记扩展,可以像这样使用:
这是此扩展的代码:
更多详细信息在这里:http://www.thomaslevesque.com/2008 /11/18/wpf-绑定到应用程序设置-使用标记扩展/
The solution above does work, but I find it quite verbose... you could use a custom markup extension instead, that could be used like this :
Here is the code for this extension :
More details here : http://www.thomaslevesque.com/2008/11/18/wpf-binding-to-application-settings-using-a-markup-extension/
@CSharper 的答案不适用于我用 VB.NET 编码的 WPF 应用程序(不是 C#,显然与 99.999% 的其他 WPF 应用程序不同),因为我收到了一个持久的编译器错误,抱怨找不到
Settings
在MyApp.Properties
命名空间中,即使重建后也不会消失。经过大量在线搜索后,对我来说有效的方法是使用在我的应用程序主窗口 XAML 文件中默认创建的
local
XAML 命名空间:...并通过它绑定到我的设置,例如以下内容(其中
MyBooleanSetting
是我在类型Boolean
的项目属性中定义的设置,范围为 User,使用默认的 Friend 访问修饰符):确保设置实际保存,一定要打电话
...在代码隐藏的某个位置(例如在
MainWindow.xaml.vb
文件的Me.Closing
事件中)。(归功于此Visual Studio 论坛帖子 获取灵感;请参阅 Muhammad Siddiqi 的回复。)
@CSharper's answer did not work for my WPF application coded in VB.NET (not C#, unlike apparently 99.999% of other WPF applications), as I got a persistent compiler error complaining that
Settings
could not be found in theMyApp.Properties
namespace, which would not go away even after rebuilding.What worked instead for me, after much searching online, was to instead use the
local
XAML namespace created by default in my application's main window XAML file:...and bind to my settings through it using something like the following (where
MyBooleanSetting
is a setting I defined in my project properties of typeBoolean
and scope User, with the default Friend access modifier):To ensure the settings are actually saved, be sure to call
...somewhere in your code-behind (such as in the
Me.Closing
event for yourMainWindow.xaml.vb
file).(Credit to this Visual Studio forum post for the inspiration; see the reply by Muhammad Siddiqi.)
以下是我绑定 UserSettings 的方法:
通过键入
propdp
生成依赖变量,然后按 Tab 两次。现在,您可以通过以下方式绑定
userSettings
:并确保在更改或退出时保存 UserSettings:
Here's how I bind the UserSettings:
Generate a dependency variable by typing
propdp
and then tab twice.Now, you can bind
userSettings
by:And make sure you save UserSettings when you change them or on Exit by: