Search code examples
c#wpfxamldata-bindinglistbox

How to synchronize a TextBox with a ListBox in WPF using IsSynchronizedWithCurrentItem


I'm starting with WPF and I have this pretty easy question:

I have a TextBlock and a Listbox that share the same DataContext. The ItemsSource of the ListBox is set to point to a property of the DataContext that its an ObservableCollection. I want that the TextBlock to contain the selected item of the Listbox. Some code:

View view = new View();
view.DataContext = new ViewModel();
view.Show();
<TextBlock Name="textBox1" Grid.Row="0" Grid.Column="0" Margin="1" Text="{Binding ¿xxx?}"></TextBlock>
<ListBox Name="listBox1" Grid.Row="1" Grid.ColumnSpan="2" Margin="1" ItemsSource="{Binding Model.BinariesToDeploy}" IsSynchronizedWithCurrentItem="True" />

Hope its clear.


Solution

  • try something like this

    Text = "{Binding ElementName=listBox1, Path=SelectedValue.Content}"