如何将实体容器(上下文)引用从父窗口传递到子用户控件?
我的 WPF 应用程序在主(父)窗口中有一个选项卡控件。每个选项卡都包含一个 UserControl,用于整理主窗口背后的 xaml 和代码。我正在使用 Julie Lerman 的这篇文章。我没有使用 MVVM。该应用程序对单个表/实体执行 CRUD 操作。使用外键引用将多个查找表/实体连接到主表。父窗口有一个类级 _context
变量,它引用实体容器的新实例,我将其视为我的数据库连接(类固醇)。 如何将 _context
从主窗口传递到用户控件?
创建引用 _context
的 Context
属性父窗口似乎是个好主意。问题是破坏了我的父窗口 xaml。它不再编译,因为我正在 UserControl 的加载事件中访问 Context
。我猜测该控件是在父窗口之前编译的,导致主窗口 xaml 中出现空引用(从子窗口到父窗口 Context)异常。如果我只是在 UserControl 中创建一个新的 _childContext 变量,一切都会正常工作,但这似乎是一个容易出错的解决方案。
我需要 _context
引用的原因是用它来填充我的下拉查找列表。所有绑定的 UserControl 字段都在父窗口中设置了其 DataContext。父 DataContext 引用正在执行 CRUD 的单个实体/表。此 DataContext 不包括我的查找表。这就是为什么我认为我需要对 _context
的引用,以便我可以使用它在 UserControl 内生成 LINQ 语句来填充我的查找列表。
提前致谢。
My WPF app has a tab control in a main (parent) window. Each tab contains a UserControl to declutter the xaml and code behind of the main window. I'm using entity framework drag-and-drop techniques outlined in this post by Julie Lerman. I am not using MVVM. The app performs CRUD operations on a single table/entity. Multiple lookup table/entities are joined to the primary table using foreign key references. The parent window has a class level _context
variable referencing a new instance of my entity container which I think of as my database connection (on steroids). How do I pass _context
from the main window to the user controls?
Creating a Context
property referencing _context
on the parent window seemed like a good idea. The problem is that breaks my parent window xaml. It no longer compiles because I'm accessing Context
in the UserControl's loaded event. I'm guessing the control is compiled before the parent window causing a null reference (from the child to the parent Context
) exception in the main window xaml. Everything works fine if I just create a new _childContext
variable in the UserControl but that seems like an error prone solution.
My reason for needing the _context
reference is to use it to populate my drop-down lookup lists. All of the bound UserControl fields have their DataContext set in the parent window. The parent DataContext references the single entity/table that CRUD is being performed against. This DataContext does not include my lookup tables. That is why I think I need a reference to _context
so I can use it to generate LINQ statements inside the UserControl to populate my lookup lists.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果将父窗口 DataContext 设置为 _context 变量,您的子窗口将自动将其继承到其 DataContext 中。然后只需更改主窗口绑定以指向 DataContext 中其感兴趣的部分,并让您的孩子使用他们感兴趣的部分。
If you set the parent windows DataContext to your _context variable your childlren will automatically inherit it into their DataContext. Then just change your main windows binding to point to the part of your DataContext that its interested in and have you children use the part they are interested in.
我找到了我自己问题的答案,而且非常简单。将实体从数据源窗口拖动到用户控件会自动生成此代码:
我意识到问题是我已经注释掉了这些生成的行,并且没有将我的数据访问代码包含在以下代码中:
if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this)) {}
。为了使一切正常工作,我恢复引用父窗口上的 Context 属性。现在我的 UserControl_Loaded 事件看起来像这样:
GetIsInDesignMode 检查更正了 MainWindow 设计器中的 xaml 编译(空引用)异常。问题解决了。
I found the answer to my own question and it's really quite simple. Dragging an entity from the Data Sources window to a UserControl auto-generates this code:
I realized the problem was that I had commented out those generated lines and had not wrapped my data access code in this:
if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this)) {}
.To make everything work I reverted back to referencing a
Context
property on the parent window. Now my UserControl_Loaded event looks something like this:The GetIsInDesignMode check corrected the xaml compile (null reference) exception in the MainWindow designer. Problem solved.