In the following example SelectedValue
of TabControl
is always null
. Why?
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib">
<DockPanel>
<TextBlock Text="{Binding SelectedValue, ElementName=Tabs}" DockPanel.Dock="Bottom"/>
<TabControl x:Name="Tabs" SelectedValuePath="Content.SelectedItem">
<TabItem Header="TabOne">
<ListView>
<ListView.View>
<GridView>
<GridViewColumn/>
</GridView>
</ListView.View>
<s:String>ItemOne</s:String>
<s:String>ItemTwo</s:String>
</ListView>
</TabItem>
<TabItem Header="TabTwo">
<ListView>
<ListView.View>
<GridView>
<GridViewColumn/>
</GridView>
</ListView.View>
<s:String>ItemOne</s:String>
<s:String>ItemTwo</s:String>
</ListView>
</TabItem>
</TabControl>
</DockPanel>
</Window>
As micahtan points out in a comment, SelectedValue
does update when you switch tabs. This means that TabControl
does not monitor properties in SelectedValuePath
for changes, only polls them every time its SelectedItem
changes.
Not sure what your trying to do, but:
Assuming that you want the name of the selected TabItem to show up in the TextBlock, it's because your SelectedValuePath is incorrect. Try changing your TabControl tag to:
<TabControl x:Name="Tabs" SelectedValuePath="Header">
Assuming that you're trying to get the string contents inside of the ListView, try changing your TextBox binding to:
<TextBlock Text="{Binding SelectedItem.Content.SelectedItem, ElementName=Tabs}" DockPanel.Dock="Bottom"/>