本文介绍了如何将索引属性绑定到 WPF 中的控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
给定类 ThisClassShouldBeTheDataContext 的实例作为视图的 Datacontext
Given an instance of the class ThisClassShouldBeTheDataContext as the Datacontext for the view
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;}
}
如何将 Contact["John"].PhoneNumber
绑定到文本框?
How can I bind Contact["John"].PhoneNumber
to a textbox?
<TextBox Text="{Binding ?????}" />
推荐答案
索引器符号与 C# 基本相同:
The indexer notation is basically the same as C#:
<TextBox Text="{Binding Contacts[John].PhoneNumber}" />
请参阅 绑定声明概述 > 绑定路径MSDN 中的语法了解更多信息.
See Binding Declarations Overview > Binding Path Syntax in MSDN for more info.
这当然不适用于任意数据类型...
This won't, of course, work for arbitrary data types...
这篇关于如何将索引属性绑定到 WPF 中的控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!