更改 WPF 中组合框的数据
因此,我有一个包含数据的 ComboBox,它可以按他们想要的方式工作:
<ComboBox Grid.Column="1" x:Name="MyComboBox" Margin="2, 0, 2, 0"
ItemsSource="{Binding Path=MySamples}" DisplayMemberPath="SampleName" SelectedValue="{Binding Path=MySample}"
SelectionChanged="OnComboBoxChanged" FontSize="11" FontFamily="Arial"/>
但是,现在他们希望对 ItemsSource 建立索引。所以它应该是这样的:
some #: SampleName
是否有一种简单的方法可以在不改变架构的情况下仅针对 ComboBox 下拉列表进行此更改?我无法更改列表本身,因为在地图的其他区域,它只是没有索引的 SampleName。谢谢。
So I have a ComboBox with data in it and it works how they want:
<ComboBox Grid.Column="1" x:Name="MyComboBox" Margin="2, 0, 2, 0"
ItemsSource="{Binding Path=MySamples}" DisplayMemberPath="SampleName" SelectedValue="{Binding Path=MySample}"
SelectionChanged="OnComboBoxChanged" FontSize="11" FontFamily="Arial"/>
However, now they want the ItemsSource to be indexed. So it should be something like:
some #: SampleName
Is there an easy way to make this change just for the ComboBox drop down without changing the architecture? I cannot change the List itself since in other areas of the map, it's just the SampleName without the index. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您的
ItemsSource
是复杂类型:则使用
ComboBox
的DisplayMemberPath
属性来控制显示的内容。在这种情况下,您需要将: 添加到
ComboBox
定义中。如果您想同时显示索引和名称,那么您需要为 ComboBox 定义一个 ItemTemplate:
If your
ItemsSource
is a complex type:Then use the
DisplayMemberPath
property of theComboBox
to control what gets displayed. In this case you'd add:to the
ComboBox
definition.If instead you want to display both the index and name then you'll need to define an ItemTemplate for the ComboBox: