views:

83

answers:

1

I have listbox that displays information about list of objects:

            <ListBox Grid.Column="0" Height="152" Name="CustomersList" HorizontalAlignment="Stretch">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Path=Name, Mode=OneWay}"/>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>

I would show more detailed information about selected item in another block, but I don't know (and can't find) how to bind selected item to those block data context. I guess it should be something like this:

                <Grid Grid.Column="1" DataContext="{Binding Path=ItemSelected, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Top">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="150"/>
                    <ColumnDefinition Width="250"/>
                </Grid.ColumnDefinitions>

                <Grid.RowDefinitions>
                    <RowDefinition />
                    <RowDefinition/>
                    <RowDefinition/>
                </Grid.RowDefinitions>

                <TextBlock Grid.Row="0" Grid.Column="0" Text="Id" VerticalAlignment="Center" />
                <TextBox Grid.Row="0" Grid.Column="1" Text="{Binding Path=Id}" VerticalAlignment="Center"/>

                <TextBlock Grid.Row="1" Grid.Column="0" Text="Name" VerticalAlignment="Center"/>
                <TextBox Grid.Row="1" Grid.Column="1" Text="{Binding Path=Name}" VerticalAlignment="Center"/>

                <StackPanel Grid.Row="2" Grid.ColumnSpan="2" Orientation="Horizontal" HorizontalAlignment="Center">
                    <Button Content="Add new" />
                    <Button Content="Store changes" />
                </StackPanel>
            </Grid>

But the problem is that data item to be binded to the grid is not specified anywhere and I don't know how to do that.

Any suggestion would be wellcome.

Thanks!

P.S. I've tried to specify CustomersList.ItemsSource as DataContext for the Grid - this didn't give any result.

+2  A: 

You can bind to the SelectedItem property of the ListBox, one way is using ElementName.

<Grid DataContext="{Binding ElementName=CustomersList, Path=SelectedItem}"> </Grid>
PMN
Thanks a lot, it works!
Budda
Then please mark PMN's answer as the right answer.
Jakob Christensen