我正在构建一个 WPF UserControl
.为此,我实现了一个 ItemSource DependecyProperty
像这样:
private IEnumerable MisItems;
public IEnumerable ItemsSource
{
get { return (IEnumerable)GetValue(ItemsSourceProperty); }
set { SetValue(ItemsSourceProperty, value); }
}
public static readonly DependencyProperty ItemsSourceProperty =
DependencyProperty.Register("ItemsSource", typeof(IEnumerable), typeof(TextBoxAutoComplete), new PropertyMetadata(new PropertyChangedCallback(OnItemsSourcePropertyChanged)));
private static void OnItemsSourcePropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
var control = sender as TextBoxAutoComplete;
if (control != null)
control.OnItemsSourceChanged((IEnumerable)e.OldValue, (IEnumerable)e.NewValue);
}
private void OnItemsSourceChanged(IEnumerable oldValue, IEnumerable newValue)
{
MisItems = newValue;
// Remove handler for oldValue.CollectionChanged
var oldValueINotifyCollectionChanged = oldValue as INotifyCollectionChanged;
if (null != oldValueINotifyCollectionChanged)
{
oldValueINotifyCollectionChanged.CollectionChanged -= new NotifyCollectionChangedEventHandler(newValueINotifyCollectionChanged_CollectionChanged);
}
// Add handler for newValue.CollectionChanged (if possible)
var newValueINotifyCollectionChanged = newValue as INotifyCollectionChanged;
if (null != newValueINotifyCollectionChanged)
{
newValueINotifyCollectionChanged.CollectionChanged += new NotifyCollectionChangedEventHandler(newValueINotifyCollectionChanged_CollectionChanged);
}
}
void newValueINotifyCollectionChanged_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
//Do your stuff here.
}
ItemsSource
属性由 IEnumerable
表示目的。现在我需要将它转换为 Dictionary<object,string
> 在这个函数中:
protected SearchResult DoSearch(string searchTerm)
{
if (!string.IsNullOrEmpty(searchTerm))
{
SearchResult sr = new SearchResult();
//var ItemsText = MisItems.GetType();
var p = (List<string>)MisItems;
/*sr.Results = ItemsText.Select((x, i) => new { x, i }).Where(x=>x.ToString().ToUpper().Contains(searchTerm.ToUpper()))
.ToDictionary(a => (object)a.i, a => a.x);*/
return sr;
}
else return new SearchResult();
}
我如何进行转换?
编辑 更多信息: 我的 View 模型具有此属性:
public List<EnumeradorWCFModel> Clientes { get; set; }
此属性的数据由 WCF service
返回:
Clientes = _svc.Clientes_Enum(sTicket, "");
然后我想要我的 UserControl
绑定(bind)到这个属性。我这样创建我的控件:
<autocomplete:TextBoxAutoComplete x:Name="Clientes" ItemsSource = "{Binding Path=Clientes}" DisplayMemberPath="Descripcion" Height="25"/>
最佳答案
[s]好的。您发布了很多代码(我个人认为对于您尝试做的事情来说是不必要的)。
让我们瘦下来。
你有一个 IEnumerable<string>
开始,对吗?好。
有一个 ToDictionary()
LINQ 库中的扩展方法。文档是 here .
所以你需要做的是:
IEnumerable<string> myEnumerableOfStrings = new List<string>();
Dictionary<object, string> dictionary = myEnumerableOfStrings.ToDictionary(value => (object) value);
And here's a Fiddle as an example.
好的,所以我们只有一个 IEnumerable
没有强类型。 (首先,我曾经看到或听说过这样做,但应该适用相同的原则。)
我们需要创建一个本地字典并迭代该集合。
var myDictionary = new Dictionary<object, string>();
IEnumerable myCollection = new List<string>();
foreach(var item in myCollection)
{
// This might be fun if you get two of the same object in the collection.
// Since this key is based off of the results of the GetHashCode() object in the base object class.
myDictionary.Add((object) item, item.ToString());
}
关于C# - 将 IEnumerable 转换为 Dictionary<object,string>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33832367/