Search code examples
wpfxamlselectedvalue

WPF: SelectedValue always null


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>

Solution

  • 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.