Trying to get a better understanding of SelectedValuePath and IsSynchronizedWithCurrentItem

Posted by rasx on Stack Overflow See other posts from Stack Overflow or by rasx
Published on 2010-03-17T18:25:46Z Indexed on 2010/03/17 19:01 UTC
Read the original article Hit count: 330

Filed under:
|

The following XAML produces a run-time Binding error when I click on an item in the ListBox:

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:sys="clr-namespace:System;assembly=mscorlib"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="WpfApplication1.MainWindow"
    x:Name="Window"
    Title="MainWindow"
    Width="640" Height="480">
    <Window.Resources>
        <x:Array x:Key="strings" Type="{x:Type sys:String}">
            <sys:String>one</sys:String>
            <sys:String>two</sys:String>
            <sys:String>three</sys:String>
            <sys:String>four</sys:String>
        </x:Array>
    </Window.Resources>
    <Grid>
        <ListBox
            DataContext="{StaticResource strings}"
            IsSynchronizedWithCurrentItem="True"
            ItemsSource="{Binding}"
            SelectedValuePath="{Binding /Length}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <Grid.Resources>
                            <Style TargetType="{x:Type Label}">
                                <Setter Property="Background" Value="Yellow"/>
                                <Setter Property="Margin" Value="0,0,4,0"/>
                                <Setter Property="Padding" Value="0"/>
                            </Style>
                        </Grid.Resources>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition/>
                            <ColumnDefinition/>
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition/>
                            <RowDefinition/>
                        </Grid.RowDefinitions>
                        <!-- Row 0 -->
                        <Label Grid.Column="0" Grid.Row="0">String:</Label>
                        <TextBlock
                            Grid.Column="1"
                            Grid.Row="0"
                            Text="{Binding}"/>
                        <!-- Row 1 -->
                        <Label Grid.Column="0" Grid.Row="1">Length:</Label>
                        <TextBlock
                            Grid.Column="1"
                            Grid.Row="1"
                            Text="{Binding Length, Mode=Default}"/>
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
</Window>

This is the run-time Binding error message:

System.Windows.Data Error: 39 : BindingExpression path error: '3' property not found on 'object' ''String' (HashCode=1191344027)'. BindingExpression:Path=3; DataItem='String' (HashCode=1191344027); target element is 'ListBox' (Name=''); target property is 'NoTarget' (type 'Object')

I would like the selected value of the ListBox to be the Length of the selected String object. What is wrong with my SelectedValuePath Binding syntax? Are there any related issues with IsSynchronizedWithCurrentItem?

© Stack Overflow or respective owner

Related posts about wpf

Related posts about xaml