ChildWindow, WindowManager and DataContext

Dec 10, 2010 at 4:39 PM
Edited Dec 10, 2010 at 4:40 PM

Hey, first i have to say excellent piece of software!

Second thing, i have a simple issue which i can go around but i'd like to hear your thoughts on it:

The problem is that binding the View inside ChildWindow basically sets the DataContext of the childwindow and not the View itself.
If there's no childwindow, the datacontext is set on the view itself. This inconsistency is a bit troubling since for me to bind the combobox items succesfully i need to know where's the datacontext.
Obvious solution is to override the bind operation and set additionally the datacontext of the view itself if it's inside the childwindow.

Would that be appropriate? Any better option?

Thanks in advance! 

Dec 10, 2010 at 5:11 PM

So, to be clear, what you would like is that we always set the DataContext on both the ChildWindow and the View so that you always know that you can get the VM from the View. Is that correct?

Dec 10, 2010 at 10:56 PM

Well, yea:)

I mean i can do it myself, not a big problem, but i was thinking that that kind of consistency (being able to rely on view's datacontext) would be nice. I lost about 3-4 hours until i figured out why my binding's not working:).  I was basically wondering would that be a problem in some other case?

Dec 11, 2010 at 5:17 AM

I don't quite understand why your bindings wouldn't work, since DataContext is inherited. Were you using a lot of relative source ancestor bindings?

Dec 11, 2010 at 9:49 AM

most of them are working fine, except the combobox itemssource binding on the dataform.

I'm setting up the binding dynamically (not through xaml) for the dataform, i have a little fluent interface for creating and binding dataform. So, when i set the binding of itemssource of a combobox, i have to set the source of the binding explicitly, to bridge the visual tree. So my binding would look something like this

Binding binding = new Binding();
binding.Source = myView;
binding.Path = new PropertyPath("DataContext.ComboBoxList");
combo.SetBinding(ItemsControl.ItemsSource, binding);
But from what i gathered, same problem shows up when using elementname and path. DataContext inheriting is not working here.
So if i set it up like this, it works only if myView contains the VM inside datacontext. I prefer this approach opposed to the other approaches you listed in the discussion here:

I  even reported that option nr. 3 works for me:) It's clean and simple, it's just you have to find the location of VM, and it's  kinda hard to pinpoint it when you have a childwindow dynamically created containing the VM:)

As i said, if i got it all wrong i'd gladly hear about other options, but i still feel that childwindow holding the VM is a bit incosistent.

Dec 13, 2010 at 1:55 PM
Edited Dec 13, 2010 at 1:55 PM

hey, nevermind, solved it, ignore the previous posts please, it's basically me being dumb and searching for solution in the wrong place. Thanks for your time