I’m working with a Panorama WP7 project, and I’m having trouble binding my viewmodels to my view. Since my view is a Panorama, I want to have one view model for the first PanoramaItem, and a second viewmodel for the second PanoramaItem.
I can get this to work if I give x:Name properties to the PanoramaItems, but I’m trying not to do that, since MVVM discourages using x:Name properties. The alternatives I’ve tried haven’t worked though. It’s easy to set the DataContext of the entire panorama to one viewmodel, but then I don’t have the data I need for the second panorama. If I try to assign the DataContext to each PanoramaItem in that items Loaded event handler, I can get a reference to the PanoramaItem through the sender parameter in the Loaded event handler, but the compiler throws an error for the second Loaded event handler – apparently you can’t assign a Loaded event to two PanoramaItems in one Panorama. I can’t assign any event to the second PanoramaItem, for that matter.
Can anyone help?
thanks,
Andy
OK, I figured it out. It’s possible to assign the DataContext to the PanoramaItems in the Panorama using the sender object in the Loaded event of the Panorama, as follows:
In this example, you have to know what your PanoramaItems are going to be, they’re not dynamic, but this method lets you refrain from x:Name’ing your controls in your XAML.