绑定到配置节

发布于 2024-07-30 20:11:05 字数 367 浏览 16 评论 0原文

我刚刚将所有设置从 AppSettings 转换为 ConfigurationSections。 它确实清理了一切,但我在使用首选项窗口时遇到了困难。 我想在 WPF 窗口中使用绑定。

  • 我是否应该将每个 ConfigurationSection 存储在依赖项属性中并绑定到 ConfigurationSection 的属性?
  • 我应该使用调用 ConfigurationManager.GetSection 的 ObjectDataProvider 吗?
  • 我还有其他方法可以做到这一点吗?

题外话:我发现 WPF 中的绑定非常强大,但有时创建绑定有点令人困惑或困难。 我希望有更好的 XAML 文档。

I just converted all my settings from AppSettings to ConfigurationSections. It definitely cleaned things up, but I'm having difficulties with the preferences window. I want to use bindings in my WPF window.

  • Should I store each of the ConfigurationSections in a dependency property and bind to the ConfigurationSection's properties?
  • Should I use ObjectDataProvider's that calls the ConfigurationManager.GetSection?
  • Is there another way I can do this?

Off-topic: I find the bindings in WPF to be really powerful, but it's sometimes a bit confusing or difficult to create the bindings. I wish there was better documentation for XAML.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雨的味道风的声音 2024-08-06 20:11:05

您不需要做任何特殊的事情 - 您可以将数据绑定到具有普通旧属性的类型。 所有有关依赖属性的内容仅适用于 WPF 控件本身。 当涉及到您绑定的模型时,没有特定的限制。 您可以绑定到普通旧 C# 对象 (POCO),尽管实现 INotifyPropertyChanged 是一个优势。

但是,与其直接绑定到 Domain 对象(听起来您的 ConfigurationSections 适合该角色),不如显式创建一个 ViewModel 来处理特定于视图的职责,同时封装真正的 Domain 对象,这通常是一个好主意。

Josh Smith 的文章模式:采用模型-视图-视图模型设计模式的 WPF 应用程序 是对 WPF 中正确数据绑定的精彩介绍。

You don't need to do anything special - you can databind to types with plain old properties. All the stuff about dependency properties are only for WPF controls themselves. When it comes to the model against which you bind, there are no particular constraints. You can bind to Plain Old C# Objects (POCOs), although implementing INotifyPropertyChanged is an advantage.

However, instead of binding directly to your Domain objects (it sounds like your ConfigurationSections fit that role), it is often a good idea to explicitly create a ViewModel that deals with view-specific responsibilities while encapsulating the real Domain objects.

Josh Smith's article Patterns: WPF Apps With The Model-View-ViewModel Design Pattern is an excellent introduction to proper databinding in WPF.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文