8

I have two usercontrols, the first with a listbox that is bound to a list of Customers that displays some simple details for each customer.

The second user control I would like to be a more detailed view of whichever customer is selected in the listbox of the first usercontrol.

Is it possible to set up a binding in the second control to bind to the selected item in the first user control?

My List box:

            <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 Usercontrol (So Far)

 <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

gr-eg
  • 306
  • 2
  • 4
  • 13
  • 1
    What you have should work if you change your TextBlock binding to Text="{Binding name}". I would suggest you change "name" to "Name" in your property declaration in Customer to identify it as a property and to make it follow some "rules!". – Dun Sep 19 '12 at 14:02

2 Answers2

4

I would suggest to have a property in your ViewModel of Customer object say SelectedCustomer and bind it to the SelectedItem of your listbox like this -

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

Since you mentioned that both user controls are in same view, so i am assuming that they share a same ViewModel. In that 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> 
Rohit Vats
  • 79,502
  • 12
  • 161
  • 185
2

Yes, you can - if you give the listbox a name of CustomerList then you can bind to its SelectedItem property using a binding like "{Binding ElementName=CustomerList, Path=SelectedItem}".

Mark Watts
  • 724
  • 7
  • 15
  • 1
    @Mark.. you solution works if both listboxes are in same user control. but as per greg he is very clear that bth listboxes are in diffrent user control – Bathineni Jul 29 '11 at 14:39
  • @greg.. are you using the both user controls in another user control or Window..? if possible try to publish ur xaml code.. atleast simplified one – Bathineni Jul 29 '11 at 14:40
  • @bathineni Yes both user controls are used in the same user control and there will only ever be one instance at a time. I have posted the xaml for the detailed view user control in my original question – gr-eg Jul 29 '11 at 14:45
  • 1
    Ah, apologies, I misread the first sentence. You could expose the SelectedItem of the ListBox via a property on Control1 and then do element-to-element binding to that from Control2. This doesn't seem as elegant as it could do, granted. – Mark Watts Jul 29 '11 at 14:48