gpt4 book ai didi

wpf - WPF 列表框上的多重绑定(bind)

转载 作者:行者123 更新时间:2023-12-04 23:53:06 30 4
gpt4 key购买 nike

如何在列表框中使用这样的代码???:

<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat="{}{0} + {1}">
<Binding Path="Name" />
<Binding Path="ID" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>

我想在列表框中执行上述操作。我的数据源有两个项目,名字和姓氏,我想同时显示这两个项目。我试过了,但它不起作用:

<ListBox ItemsSource="{Binding}" Name="listBox" Width="200" DockPanel.Dock="Left" VerticalAlignment="Stretch" VerticalContentAlignment="Stretch" SelectionChanged="selectionChanged" >
<ListBox.DisplayMemberPath>
<MultiBinding StringFormat="{}{0} + {1}">
<Binding Path="forenames" />
<Binding Path="surname" />
</MultiBinding>
</ListBox.DisplayMemberPath>
</ListBox>

请注意,我想像上面那样使用 xaml,而不是单独的 IConverter 类或其他东西。

最佳答案

使用自定义 ItemTemplate 而不是 DisplayMemberPath

<ListBox ItemsSource="{Binding}" Name="listBox" Width="200" DockPanel.Dock="Left" VerticalAlignment="Stretch" VerticalContentAlignment="Stretch" SelectionChanged="selectionChanged" >
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat="{}{0} + {1}">
<Binding Path="forenames" />
<Binding Path="surname" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>

关于wpf - WPF 列表框上的多重绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23826790/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com