Note that there are some explanatory texts on larger screens.

plurals
  1. POBind to ListBox selectedItem inside TabControl
    primarykey
    data
    text
    <p>Okay, I got this Tabcontrol containing a ListBox. Now my problem it that I would like to bind <code>&lt;TextBox x:Name="DetailTextBox" Text="{Binding Detail}"/&gt;</code> to the selectedItem in the listbox and show the <code>Detail</code> property value. Note that the TextBox is not part of the TabControl, but is in another Column. </p> <p>I can't quite figure out, how to handle binding, when there a multiple ListBox'es, one in each TabControl Item.</p> <p>My classes</p> <pre><code>public class TabViewModel { public string Name { get; set; } public ObservableCollection&lt;TabItemViewModel&gt; Collection { get; set; } } public class TabItemViewModel { public string Title { get; set; } public string Detail { get; set; } } </code></pre> <p>public MainWindow()</p> <pre><code> var tabViewModels = new ObservableCollection&lt;TabViewModel&gt;(); tabViewModels.Add(new TabViewModel{Name = "Tab 1", Collection = new ObservableCollection&lt;TabItemViewModel&gt;{new TabItemViewModel{Detail = "Detail 1.1", Title = "Title 1.1"}, new TabItemViewModel{Detail = "Detail 2.2", Title = "Title 2.2"}}}); tabViewModels.Add(new TabViewModel { Name = "Tab 2", Collection = new ObservableCollection&lt;TabItemViewModel&gt; { new TabItemViewModel { Detail = "Detail 2.1", Title = "Title 2.1" }, new TabItemViewModel { Detail = "Detail 2.2", Title = "Title 2.2" } } }); tabViewModels.Add(new TabViewModel { Name = "Tab 3", Collection = new ObservableCollection&lt;TabItemViewModel&gt; { new TabItemViewModel { Detail = "Detail 3.1", Title = "Title 3.1" }, new TabItemViewModel { Detail = "Detail 3.2", Title = "Title 3.2" } } }); DataContext = tabViewModels; </code></pre> <p>MainWindow.xaml.</p> <pre><code>&lt;Grid&gt; &lt;Grid.ColumnDefinitions&gt; &lt;ColumnDefinition Width="50*"/&gt; &lt;ColumnDefinition Width="50*"/&gt; &lt;/Grid.ColumnDefinitions&gt; &lt;TabControl ItemsSource="{Binding}" Grid.Column="0" SelectedIndex="0"&gt; &lt;TabControl.ItemContainerStyle&gt; &lt;Style TargetType="{x:Type TabItem}"&gt; &lt;Setter Property="Header"&gt; &lt;Setter.Value&gt; &lt;Binding Path="Name"/&gt; &lt;/Setter.Value&gt; &lt;/Setter&gt; &lt;/Style&gt; &lt;/TabControl.ItemContainerStyle&gt; &lt;TabControl.ContentTemplate&gt; &lt;DataTemplate&gt; &lt;ListBox ItemsSource="{Binding Collection}"&gt; &lt;ListBox.ItemTemplate&gt; &lt;DataTemplate&gt; &lt;Label Content="{Binding Title}" /&gt; &lt;/DataTemplate&gt; &lt;/ListBox.ItemTemplate&gt; &lt;/ListBox&gt; &lt;/DataTemplate&gt; &lt;/TabControl.ContentTemplate&gt; &lt;/TabControl&gt; &lt;StackPanel Grid.Column="1"&gt; &lt;TextBox x:Name="DetailTextBox" Text="{Binding Detail}"/&gt; &lt;/StackPanel&gt; &lt;/Grid&gt; </code></pre> <p><img src="https://i.stack.imgur.com/CewUo.png" alt="enter image description here"></p> <p><strong>EDIT</strong></p> <p><strong>Temp Solution</strong> Found a way to make it work, but I'm still looking for a pure Xaml solution. Added a SelectionChange event</p> <pre><code> &lt;ListBox ItemsSource="{Binding Collection}" SelectionChanged="ListBox_SelectionChanged"&gt; private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { if (e.AddedItems.Count &gt; 0) DetailTextBox.DataContext = (TabItemViewModel) e.AddedItems[0]; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload