DependencyProperty Binding Problem
-
I have a UserControl on a TabView that has a DependencyProperty of type Project. Project is an object.
public static readonly DependencyProperty ProjectProperty =
DependencyProperty.Register("Project",
typeof(ProjectEntity),
typeof(PurchaseOrderView),
new PropertyMetadata(null, new PropertyChangedCallback(OnProjectChanged)));public ProjectEntity Project
{
get { return (ProjectEntity)GetValue(ProjectProperty); }
set { SetValue(ProjectProperty, value); }
}private static void OnProjectChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
PurchaseOrderView control = (PurchaseOrderView)d;
control.LoadPurchaseOrders();
}
#endregionWhen the control is first loaded, and the binding occurs, Project is set to the instance, and everything works fine. However, when I select another tab, the control's CTOR and DP's Changed method fire AGAIN and now Project is Null and casues null ref exceptions. I'm not really sure what's going on. Is it unbinding somehow?? I don't really know how to diagnose this. I found [this article](https://stackoverflow.com/questions/3601125/wpf-tabcontrol-preventing-unload-on-tab-change) that discusses the Tab Control removing and replacing its content when the tabs change, and I tried using the solution, but it doesn't work. Anyone have an ideas?
If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.