【发布时间】:2015-07-16 13:27:45
【问题描述】:
我有一个“组合框”用户控件/小部件/视图 (xamarin.formsPicker),其中 ItemsSource 可以是列表或字典。 ComboBox 在它们之间具有以下属性和 sycs:
SelectedItem : object;
SelectedValue : object;
SelectedValuePath : string;
DisplayMemberPath : string;
“组合框”(Picker) 也有一些内部属性:
SelectedIndex : int;
Items : IList<string>;
ItemsSource 当前没有类型化 IEnumerable - 这样您就可以使用 Dictionary 或 List 从 XAML 填充它。
当我尝试通过知道 SelectedIndex 来设置 SelectedItem 时出现问题。
我不知道Dictionary 的类型(即Dictionary<int, string>)。使用列表可以正常工作:
this.SelectedItem = ((IEnumerable<object>)this.ItemsSource).ToArray()[this.SelectedIndex];
为了使用 Enumerable.ElementAt({index}),我必须将 IEnumerable 转换为类型化字典。它不适用于 IDictionary/Dictionary,而且我似乎也无法转换为 Dictionary<dynamic, dynamic>。
此外,我需要从 SelectedItem 更改中同步回 SelectedIndex,这会带来类似的问题。有了一个列表,我就可以做到
this.SelectedIndex = this.ItemsSource.IndexOf(this.SelectedItem);
这不会为 Dictionary 抛出异常,但会返回 -1。
【问题讨论】:
标签: xamarin.forms c# dictionary combobox ienumerable xamarin.forms