How do I pass an entity container (context) reference from the parent window to child user controls?
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 longe开发者_Go百科r 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.
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.
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.
精彩评论