如何在 TextBox 的 ListBoxes 多重绑定中显示 属性 项的值

How to display values of a property from item in ListBoxes multibinding in a TextBox

我有一个 TextBox,我想在后面的代码中显示所选项目的 属性 的值(有一些事件,例如 MouseDown

属性 是 Int16 类型。

我在这里看到了类似的问题,但是:

TextBox2.Text = ((Helmet) listhelmets.SelectedItems).protection;

对我不起作用,所以我看到的其他建议也不适用。

<ListBox x:Name="listweapons" Height="214" Width="248" ItemsSource="{Binding ListWeapon}" 
         IsSynchronizedWithCurrentItem="True" Canvas.Left="211" Canvas.Top="72" 
         PreviewMouseLeftButtonDown="weapons_PreviewMouseLeftButtonDown"
         PreviewMouseMove="weapons_PreviewMouseMove" 
         SelectedValuePath="attack">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <Image Source="{Binding Path=Image}" Width="56" Height="61"/>
                <TextBox Height="30" Width="30">
                    <Binding Path="_attack" />
                </TextBox>
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

您不能将类型 ListBox.SelectedItemCollection 的对象转换为类型 Helmet,因此您应该使用 listhelmetsSelectedItem 属性 而不是 SelectedItems 像这样:

TextBox2.Text = ((Helmet) listhelmets.SelectedItem).protection; 

如果 protection 不是 string,也不要忘记在行尾使用 ToString()