如何将实体容器(上下文)引用从父窗口传递到子用户控件?

发布于 2024-11-08 20:27:51 字数 895 浏览 0 评论 0原文

我的 WPF 应用程序在主(父)窗口中有一个选项卡控件。每个选项卡都包含一个 UserControl,用于整理主窗口背后的 xaml 和代码。我正在使用 Julie Lerman 的这篇文章。我没有使用 MVVM。该应用程序对单个表/实体执行 CRUD 操作。使用外键引用将多个查找表/实体连接到主表。父窗口有一个类级 _context 变量,它引用实体容器的新实例,我将其视为我的数据库连接(类固醇)。 如何将 _context 从主窗口传递到用户控件?

创建引用 _contextContext 属性父窗口似乎是个好主意。问题是破坏了我的父窗口 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 技术交流群。

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

发布评论

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

评论(2

亽野灬性zι浪 2024-11-15 20:27:51

如果将父窗口 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.

话少心凉 2024-11-15 20:27:51

我找到了我自己问题的答案,而且非常简单。将实体从数据源窗口拖动到用户控件会自动生成此代码:

private void UserControl_Loaded(object sender, RoutedEventArgs e)
{            
     //Do not load your data at design time.
     if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
     {
        //Load your data here and assign the result to the CollectionViewSource.
        System.Windows.Data.CollectionViewSource myCollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["Resource Key for CollectionViewSource"];
        myCollectionViewSource.Source = your data
     }
}

我意识到问题是我已经注释掉了这些生成的行,并且没有将我的数据访问代码包含在以下代码中:

if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this)) {}

为了使一切正常工作,我恢复引用父窗口上的 Context 属性。现在我的 UserControl_Loaded 事件看起来像这样:

// Do not load your data at design time.
if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
{
    Window parentWindow = Window.GetWindow(this);
    MainWindow mainWindow = (MainWindow)parentWindow;
    MyEntities context = mainWindow.Context;

    var lookupList = from c in context.MyEntity
                     select c;

    System.Windows.Data.CollectionViewSource myEntitiesViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("myEntitiesViewSource")));
    // Load data by setting the CollectionViewSource.Source property:
    myEntitiesViewSource.Source = lookupList;

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:

private void UserControl_Loaded(object sender, RoutedEventArgs e)
{            
     //Do not load your data at design time.
     if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
     {
        //Load your data here and assign the result to the CollectionViewSource.
        System.Windows.Data.CollectionViewSource myCollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["Resource Key for CollectionViewSource"];
        myCollectionViewSource.Source = your data
     }
}

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:

// Do not load your data at design time.
if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
{
    Window parentWindow = Window.GetWindow(this);
    MainWindow mainWindow = (MainWindow)parentWindow;
    MyEntities context = mainWindow.Context;

    var lookupList = from c in context.MyEntity
                     select c;

    System.Windows.Data.CollectionViewSource myEntitiesViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("myEntitiesViewSource")));
    // Load data by setting the CollectionViewSource.Source property:
    myEntitiesViewSource.Source = lookupList;

The GetIsInDesignMode check corrected the xaml compile (null reference) exception in the MainWindow designer. Problem solved.

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