WPF - Binding to a selected list item between user controls

I have two user controls, the first of which contains a list that is tied to a list of clients that displays some simple information for each client.

The second user control, which I would like to get a more detailed idea of ​​which client is selected in the list of the first usercontrol.

Is it possible to set the snap in the second control to snap to the selected item in the first user control?

My list:

<ListBox Name="lstCustomer" ItemsSource="{Binding Customers}" > <ListBox.Resources> <DataTemplate DataType="{x:Type MyApplication:Customers}"> <Label Grid.Row="0" Content="{Binding Customer.name}" FontSize="14" FontWeight="Bold" Padding="5" /> <Label Grid.Row="1" Grid.Column="0" Content="{Binding Customer.telephone}" Padding="10,5" /> </Grid> </Grid> </DataTemplate> </ListBox.Resources> </ListBox> 

Detailed view of Usercontrol (still)

  <Grid x:Name="containingGrid" DataContext="{Binding ElementName=lstCustomers, Path=SelectedItem}"> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto"/> <ColumnDefinition Width="Auto"/> </Grid.ColumnDefinitions> <TextBlock Text="{Binding Customer.name}" FontSize="23"/> </Grid> 

Thanks Greg

+7
source share
2 answers

I would suggest that the property of your ViewModel Customer object display SelectedCustomer and bind it to the SelectedItem of your list, for example:

 <ListBox Name="lstCustomer" ItemsSource="{Binding Customers}" SelectedItem = "{Binding SelectedCustomer}" > . . . . . </ListBox> 

Since you mentioned that both user controls are in the same view, so I assume they have the same ViewModel. In this case, you can simply set the data context this way -

 <Grid x:Name="containingGrid" DataContext="{Binding SelectedCustomer}"> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto"/> <ColumnDefinition Width="Auto"/> </Grid.ColumnDefinitions> <TextBlock Text="{Binding Name}" FontSize="23"/> </Grid> 
+3
source

Yes, you can - if you list CustomerList in the list, then you can bind it to the SelectedItem property using a binding like "{Binding ElementName = CustomerList, Path = SelectedItem}".

+1
source

All Articles