c# - 如何在 TextBox 的 ListBoxes 多重绑定(bind)中显示项目的属性值

标签 c# wpf listbox selecteditem

我有一个 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 属性listhelmets 而不是像这样的 SelectedItems:

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

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

关于c# - 如何在 TextBox 的 ListBoxes 多重绑定(bind)中显示项目的属性值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34474709/

相关文章:

C# 4.0 异步任务结果

c# - 为什么 WPF 中任何元素的 TabIndex 属性都是 2147483647?

WPF Datagrid 列宽度在列表框中失败

excel - 如何使用矩阵填充 Excel 中的列表框?

c# - 如果我们使用 NGen.exe 将其编译为 native 代码,C# 是否需要运行 .NET 框架?

c# - 如何在同时删除项目的同时遍历列表?

c# - 获取excel文件的名称

c# - 将 Command CanExecute 和 Executed 处理程序置于主窗口类之外的问题

wpf - 为什么这个 WPF RoutedCommand 绑定(bind)的 Context MenuItem 被禁用?

c# - ListBox 未获取选定的项目