Blend 4 Passing Data Context with Navigate To

1k Views Asked by At

I have a list of customers with various pieces of information. I have a list box with their names. When I select an entry I see more information about the customer on the screen. I want to "Navigate To" another screen when clicking on the user's name with more of their information. I can't figure out how to pass information about the entry to the next screen to accomplish this.

Here is the list box that the user chooses from to begin with.

<ListBox x:Name="scheduleListBox" 
     ItemTemplate="{DynamicResource ItemTemplate}" 
     ItemsSource="{Binding Collection}" 
     Margin="8,8,8,0" 
     Style="{DynamicResource ListBox-Sketch}" 
     Height="154" 
     VerticalAlignment="Top"/>

Here is the TextBlock that could be clicked to go to the other screen. It is changed based on what the user selected from the ListBox.

<TextBlock Text="{Binding Customer}" 
     HorizontalAlignment="Left" 
     VerticalAlignment="Top" 
     Width="150" Margin="104,0,0,0"    
     Style="{DynamicResource BasicTextBlock-Sketch}">
     <i:Interaction.Triggers>
         <i:EventTrigger EventName="MouseLeftButtonDown">
             <pi:NavigateToScreenAction  TargetScreen="V02Screens.Customer_Status"/>
         </i:EventTrigger>
     </i:Interaction.Triggers>
</TextBlock>

I'm kind of hoping that there is something I can do in Expression Blend 4 or in the XAML.

2

There are 2 best solutions below

4
On

In Windows 8, you can pass the entire object to the receiving page.

Like this:

// main page
private void ListBox_SelectionChanged_1
    (object sender, SelectionChangedEventArgs e)
{
    var _Item = (sender as ListBox).SelectedItem;
    Frame.Navigate(typeof(MainPage), _Item);
}

// detail page
protected override void OnNavigatedTo(NavigationEventArgs e)
{
    this.DataContext = e.Parameter;
}

In WPF & SL, you can save reference to the SelectedItem in your View Model.

// main page
private void ListBox_SelectionChanged_1
    (object sender, SelectionChangedEventArgs e)
{
    var _Item = (sender as ListBox).SelectedItem;
    MyModel.SelectedItem = _Item;
    // TODO: navigate
}

// detail page
protected override void OnNavigatedTo(NavigationEventArgs e)
{
    this.DataContext = MyModel.SelectedItem;
}

I hope this helps.

2
On

In WPF you can supply an object to the Navigate command which contains anything you want, including whatever data you might want to show on the next page. Then on the target page (the one you navigate to), you have to handle the load completed event.

In your first page you might navigate with...

this.NavigationService.Navigate( somePage, someContainerObject );

Then you might retrieve it on somePage with...

// Don't forget to subscribe to the event!
this.NavigationService.LoadCompleted += new LoadCompletedEventHandler(container_LoadCompleted);
...
void container_LoadCOmpleted( object sender, NavigationEventArgs e)
{
   if( e.ExtraData != null )
      // cast e.ExtraData and use it
}