我有一个 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
,因此您应该使用 的
而不是像这样的 SelectedItem
属性listhelmetsSelectedItems
:
TextBox2.Text = ((Helmet) listhelmets.SelectedItem).protection;
如果 protection
不是 string
,也不要忘记在行尾使用 ToString()
。
关于c# - 如何在 TextBox 的 ListBoxes 多重绑定(bind)中显示项目的属性值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34474709/