Wpf 试图更好地了解SelectedValuePath和IsSynchronizedWithCurrentItem

Wpf 试图更好地了解SelectedValuePath和IsSynchronizedWithCurrentItem,wpf,xaml,Wpf,Xaml,当我单击列表框中的项目时,以下XAML产生运行时绑定错误: <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="WpfApplicati

当我单击
列表框中的项目时,以下XAML产生运行时绑定错误:

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

一
二
三
四
字符串:
长度:
这是运行时绑定错误消息:

System.Windows.Data错误:39:BindingExpression路径错误:在“对象”“字符串”(HashCode=1191344027)上找不到“3”属性。BindingExpression:Path=3;DataItem='String'(HashCode=1191344027);目标元素是'ListBox'(名称='');目标属性为“NoTarget”(类型为“Object”)


我希望
列表框
的所选值是所选
字符串
对象的长度。我的
SelectedValuePath
绑定语法有什么问题?
IsSynchronizedWithCurrentItem是否存在任何相关问题?

简短回答

替换

SelectedValuePath="{Binding /Length}"

长答案

SelectedValuePath是一个字符串,提供从对象到选定值的路径。通过将
SelectedValuePath=“{Binding/Length}”
写入所选项目的“Length”属性,您将SelectedValuePath(而不是selectedValue)绑定到所选项目的“Length”属性,因此如果所选项目的长度为3,则SelectedValuePath属性的值将设置为字符串“3”。然后WPF试图通过在字符串上找到名为“3”的属性来计算SelectedValue。由于string对象没有名为“3”的属性,因此会出现错误

你可能会认为
SelectedValue={Binding/Length}“
会起作用,而且它确实表达了你实际要做的事情的概念。但是它实际上不起作用,因为
SelectedValue
的代码在
SelectedItem
更改时会覆盖
SelectedValue

另一种方法是将
SelecteValuePath
设置为值“abcd”实际上等同于将
SelectedValue
设置为
“{Binding/abcd}”
(但仅当IsSynchronizedWithCurrentItem=“true”)时

SelectedValuePath="Length"