Share DataContext among navigation pages in Silverlight 3
| Comments- | Posted in
- silverlight
- xaml
- databinding
- binding
- navigation
- datacontext
- share data
- riaservices
I got an email the other day about if there was a way to pass an object between the navigation pages in Silverlight 3. The scenario was that the developer wanted to use the same data, but represent it visually in different ways.
Silverlight 3 introduces a new navigation framework in the runtime making it easier to navigate to different areas of an application and assist in ‘deep linking’ concepts for applications. More resources:
At first my reaction was “no, we don’t allow that easily” but then I thought about it a bit and played around with a sample in the context of this developer’s use case. Right now, the way you can quickly pass chunks of data to another page is via query string mechanisms. So in my code I can say:
1: myFrame.Navigate(new Uri("/foo.xaml?customerId=1234", UriKind.Relative));
And then in the navigated page something like:
1: string customerId = this.NavigationContext.QueryString["customerid"];
This works fine for string/simple data. In fact I could probably serialize a simple type and send it this way as well…but I don’t think that was the desired intent of this developer. But if you think about the concept of the Frame, then you can start thinking about DataBinding techniques and how we can use the Frame as our container. Allow me to think out loud…
Let’s use the default Silverlight Navigation Application template in the Silverlight 3 Tools for Visual Studio. This will give us enough stub to work with. If you look at MainPage.xaml you’ll see that the page has one Frame element in it:
1: <navigation:Frame x:Name="Frame" Source="/Views/HomePage.xaml"
2: HorizontalContentAlignment="Stretch"
3: VerticalContentAlignment="Stretch"
4: Padding="15,10,15,10"
5: Background="White"/>
This is the core element that is going to receive navigation commands and change its content based on those commands. Now in the Loaded event handler of MainPage, let’s grab some data. I’m using a simple class here that just iterates a Person type (mainly so I can include it in the sample easily). My Loaded event handler now looks like this:
1: void MainPage_Loaded(object sender, RoutedEventArgs e)
2: {
3: People p = new People();
4: List<Person> peeps = p.GetPeople();
5:
6: this.Frame.DataContext = peeps;
7: }
So you can see that I’m now setting the DataContext of the Frame element to my List<Person>. So now let’s crack open some of the other pages. Again, remember the scenario: same data, different views. Open the Views/HomePage.xaml and let’s show a view of just a simple list of names. I added a ListBox and just displayed the FullName property of my data:
1: <StackPanel>
2: <TextBlock Text="Name List" Style="{StaticResource HeaderTextStyle}"/>
3: <StackPanel Style="{StaticResource ContentTextPanelStyle}">
4: <ListBox x:Name="PeopleList" DisplayMemberPath="FullName"
5: ItemsSource="{Binding}" />
6: </StackPanel>
7: </StackPanel>
Notice that there is going to be no code here in the source file for HomePage.xaml.cs – we are using the {Binding} property for the ItemsSource…essentially trickling down the DataContext from the Frame. Remember, that HomePage.xaml is essentially a child control of the Frame so it is aware of the DataContext. Now let’s go into AboutPage.xaml for our more detailed view, showing a DataGrid of all the elements of the data:
1: <StackPanel>
2: <TextBlock Text="Detail" Style="{StaticResource HeaderTextStyle}"/>
3: <TextBlock Text="Detail list of members with gender."
4: Style="{StaticResource ContentTextStyle}"/>
5: <data:DataGrid ItemsSource="{Binding}"/>
6: </StackPanel>
Again, no code here to retrieve the data or wire it up – using {Binding} to the DataContext again. So now with one data fetch and binding to our navigation context (Frame) we can re-use that same object across different views and the result shows us different levels of detail.
So at least that is one thought of how to share the information. What do you think? Obviously it will not work in all scenarios, but also remember you can have more than one navigation frame in your application too! Here’s the sample code for these thoughts: SilverlightApplication32.zip
Hope this helps!
Please enjoy some of these other recent posts...
Comments