views:

619

answers:

2

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>
A: 

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"/>
micahtan
I clarified my question.
CannibalSmith
You should be able to use either of the snippets I provided to get what you want. As far as SelectedValue evaluating to null, it isn't. Try selecting one of the strings in either Tab One or Two, then change Tabs back-and-forth. I assume that it's not updating because of the binding mechanism of SelectedValue (e.g. it doesn't re-evaluate the same way that Path does).
micahtan
A: 

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.

CannibalSmith