Lan*_*nce 19 wpf xaml binding indexer properties
给定一个类ThisClassShouldBeTheDataContext的实例作为视图的Datacontext
class ThisClassShouldBeTheDataContext
{
public Contacts Contacts {get;set;}
}
class Contacts
{
public IEnumerable<Person> Persons {get;set;}
public Person this[string Name]
{
get
{
var p = from i in Persons where i.Name = Name select i;
return p.First();
}
}
}
class Person
{
public string Name {get;set;}
public string PhoneNumber {get;set;}
}
Run Code Online (Sandbox Code Playgroud)
如何绑定Contact["John"].PhoneNumber到文本框?
<TextBox Text="{Binding ?????}" />
Run Code Online (Sandbox Code Playgroud)
ito*_*son 29
索引符号与C#基本相同:
<TextBox Text="{Binding Contacts[John].PhoneNumber}" />
Run Code Online (Sandbox Code Playgroud)
有关详细信息,请参阅MSDN中的绑定声明概述>绑定路径语法.
当然,这不适用于任意数据类型......