可观察的集合绑定到特定项目(按属性值) [英] Observable collection bind to specific item (by property value)

查看:63
本文介绍了可观察的集合绑定到特定项目(按属性值)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对在wpf应用程序中将特定的可观察(或列表)项绑定到文本块有疑问.通常,您可以绑定特定项目,例如

I have a question about binding a specific observable (or list) item to a textblock in my wpf application. Normally, you can bind a specific item like this

但是,当您的收藏集发展为(例如)庞大的收藏集时.绑定属性,例如:

But when your collection grows to (for example) a huge collection. Binding properties like:

Mycollection[14224].Name 

只会变得一团糟.还有一种替代的绑定方法,它仍然可以让我绑定到可观察集合中的特定项目.但是不会通过集合中项目的索引来执行此操作.如果是这样,该怎么做?

Wil just become a mess. So is there an alternative binding method, that still lets me bind to a specific item in my observable collection. But does not do so by the index of the item in the collection. If so, how is this done?

只是为了额外的清晰度:

Just for extra clearity:

(以"semi"伪表示)

(in 'semi' pseudo)

Public class symbol
{
   Public string Name {get; set;}
   Public string Value {get; set;}

   Public symbol(string name, string value)
   {
       this.Name = name;
       this.Value = value;
   }
}

Public class viewmodel : BaseViewModel
{

Public ObservableCollection<Symbol> Symbols{get;set;}

Public viewmodel()
{
Symbols = new ObservableCollection<Symbol>();
Symbols.Add(new symbol("a","a"));
Symbols.Add(new symbol("b","b"));
//..etc etc..

}

}

后面的代码:

DataContext = new viewmodel();

在xaml中:

<TextBlock Text="{Binding Symbols[0].Value, Mode=TwoWay}"></TextBlock>

我想要的是绑定到此集合中符号的值.但是要通过其名称(字符串属性名称)来实现.类似:

What i want is, to bind to the value of a symbol in this collection. but to do this by its name (the string property name).Something like:

<Textblock Text="Binding Symbols.a.Value, Mode=TwoWay}"></Textblock>

当然,上面的绑定无效,但它只是向大家展示我正在寻找什么.

Ofcourse the binding above does not work, but its just to show you guys what im looking for.

推荐答案

为什么不编写IValueConverter并为其指定标准(例如名称)作为参数.

Why not write an IValueConverter and give it the criteria (e.g. Name) as a parameter.

可以在此处找到基本教程

如何传递参数是此处

这篇关于可观察的集合绑定到特定项目(按属性值)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆