在WPF窗口与其用户控件之间共享数据的最佳方法是什么? [英] What is the best way to share data between a WPF window and its User Controls?

查看:60
本文介绍了在WPF窗口与其用户控件之间共享数据的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

WPF有时会令人发怒.

WPF can be infuriating sometimes.

我有一个相当简单的应用程序,该应用程序由一个主窗口组成,该主窗口包含一个选项卡控件和几个选项卡.我不喜欢在同一文件中包含所有选项卡的代码的想法,因此我使用了

I have a fairly simple application that consists of a single main window that contains a tab control and several tabs. I did not like the idea of having the code for all tabs in the same file, so I used the answer from this question to break out each tab into a separate user control.

在主窗口中,我有一个对象实例,其中包含应用程序设置和一些其他应用程序范围的数据.我的几个选项卡需要访问此数据以用于数据绑定.我一直找不到找到实现此目标的好方法.

Inside my main window I have an instance of an object that contains application settings, and some other application-wide data. Several of my tabs require access to this data for data binding purposes. I have not been able to find a good way to accomplish this.

首先,我尝试访问控件Loaded事件中的父窗口,并获得对暴露设置对象的主窗口中的属性的引用,如下面的代码所示.这种工作方式是每次选项卡获得焦点时都会触发Loaded事件.另外,此事件发生在控件生命周期的后期,因此我无法绑定到用户控件XAML中此对象的任何属性.

First I tried to access the parent window in the controls Loaded event and get a reference to the property in the main window that exposed the settings object, as shown in the code below. This kind of works, except the Loaded event is fired every time the tab gains focus. Also, this event occurs late in the control life cycle so I am not able to bind to any of the properties in this object in the user controls XAML.

private void MyUserControl_Loaded(object sender, RoutedEventArgs e)
{
    this.ApplicationSettings = ((MainWindow)Window.GetWindow(this)).ApplicationSettings;
}

然后我尝试将数据传递到用户控件构造函数中,但是在XAML中无法做到这一点.

Then I experimented with passing the data into the user controls constructor, but there is no way to do that in XAML.

鉴于这些是应用程序范围的设置,因此我可以使ApplicationSettings类成为单例并在各处引用它,但出于单元测试的目的,我不希望这样做.

Given that these are application-wide settings I could make the ApplicationSettings class singleton and reference it everywhere, but I would prefer not to do that for unit testing purposes.

那么一个人如何完成这样的事情呢?我的方法是否存在根本缺陷?在我看来,所有这些UI元素都是同一窗口的一部分,因此应该能够从主窗口访问数据,但是对象模型似乎不允许这样做.

So how does one accomplish something like this? Is my approach just fundamentally flawed? In my mind all of these UI elements are part of the same window and therefore should be able to access data from the main window, but the object model does not appear to allow this.

推荐答案

您可以将设置对象放入包装类(在本示例中为 SettingsHolder )的静态属性中,并通过

You could put the settings object into a static property in a wrapper class (SettingsHolder in this example and reference it application wide via

在XAML中:

{Binding SettingName, Source={x:Static local:SettingsHolder.Settings}}

其中 local 是您的 SettingsHolder 类所在的名称空间.

with local being the namespace your SettingsHolder class is in.

使用代码:

var x = SettingsHolder.Settings.SettingName;
SettingsHolder.Settings.SettingName = x;

这篇关于在WPF窗口与其用户控件之间共享数据的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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