【问题标题】:IEnumerable<KeyValuePair<string,string>> in DisplayMemberPathDisplayMemberPath 中的 IEnumerable<KeyValuePair<string,string>>
【发布时间】:2011-03-31 11:34:25
【问题描述】:

如何在 DisplayMemberPath 中显示它 所以假设我有财产

public IEnumerable<KeyValuePair<string, string>> Defaults{ get { return defaults; } }

在 QuestionsFile 类中,我想把它放在下面的 ListBox 中

<ListBox ItemsSource="QuestionsFile.Defaults" DisplayMemberPath="?"/>

【问题讨论】:

  • 您到底想显示什么?关键和价值?您为什么不只使用具有两个属性的 POCO,这些属性为您的 KeyValuePair 所代表的对象适当命名?
  • 这只是我想展示的价值..但我会这样做

标签: c# .net xaml listbox ienumerable


【解决方案1】:

您应该将 DisplayMemberPath 设置为 Key 或 Value :

<ListBox x:Name="lst" DisplayMemberPath="Value"/>

并在后面的代码中分配 ItemSource:

lst.ItemsSource = Defaults; 

或在 xaml 中:

<ListBox ItemsSource="{Binding Defaults, RelativeSource={RelativeSource FindAncestor, AncestorType=Window}}" x:Name="lst" DisplayMemberPath="Value"/>

来源:

【讨论】:

  • 您提议使用字符串作为 ItemsSource:mathieu,您的意思是在此处添加绑定吗?
  • 但这和我做的不一样吗?我只是在 xaml 中完成的?
  • 找到了:我按照 mathieu 说的做了,但我将默认设置为 Enumerable
【解决方案2】:
<ListBox ItemsSource="QuestionsFile.Defaults" DisplayMemberPath="?"/>

你是这个意思吗?

<ListBox ItemsSource="{Binding Path=Defaults,ElementName=thisCtl}" DisplayMemberPath="?"/>

(假设 thisCtl 是 QuestionFile 控件的x:Name 属性值)

【讨论】:

  • '?'只是为了填写必须存在的内容;)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-06-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-26
相关资源
最近更新 更多