wpf C# - 将 IEnumerable 转换为 Dictionary<object,string>
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33832367/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# - Convert IEnumerable to Dictionary<object,string>
提问by ericpap
I am building a WPF UserControl. For this I implemented an ItemSource DependecyPropertylike this:
我正在构建一个 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.
}
The ItemsSourceproperty is represented by a IEnumerableObject. Now I need to convert it to a Dictionary<object,string> in this function:
该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();
}
How can i make the transition?
我该如何进行过渡?
EDITMore info: My viewmodel has this property:
编辑更多信息:我的视图模型有这个属性:
public List<EnumeradorWCFModel> Clientes { get; set; }
The data for this property is returned by a WCF service:
此属性的数据由 a 返回WCF service:
Clientes = _svc.Clientes_Enum(sTicket, "");
Then I wanted my UserControlto bind to this property. I create my control like this:
然后我希望我UserControl绑定到这个属性。我像这样创建我的控件:
<autocomplete:TextBoxAutoComplete x:Name="Clientes" ItemsSource = "{Binding Path=Clientes}" DisplayMemberPath="Descripcion" Height="25"/>
回答by Cameron
[s]Alright. You posted a lot of code (that I personally think is unnecessary for what you're trying to do).
[s]好的。您发布了很多代码(我个人认为这对于您要执行的操作是不必要的)。
Let's slim it down.
让我们瘦下来。
You have an IEnumerable<string>to start out, correct? Good.
你有一个IEnumerable<string>开始,对吗?好的。
There's a ToDictionary()extension method in the LINQ libraries. Documentation is here.
ToDictionary()LINQ 库中有一个扩展方法。文档在这里。
So what you need to do is the following:
所以你需要做的是:
IEnumerable<string> myEnumerableOfStrings = new List<string>();
Dictionary<object, string> dictionary = myEnumerableOfStrings.ToDictionary(value => (object) value);
And here's a Fiddle as an example.
Alright, so we have just an IEnumerablewith no strong type. (First I've ever seen or heard of this being done, but the same principles should apply.)
好的,所以我们只有一个IEnumerable没有强类型。(首先我见过或听说过这样做,但应该适用相同的原则。)
We need to create a local dictionary and iterate over that collection.
我们需要创建一个本地字典并遍历该集合。
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());
}

