开发者

How can you bind an Indexed property to a control in WPF

开发者 https://www.devze.com 2022-12-10 16:11 出处:网络
Given an instance of the class ThisClassShouldBeTheDataContext as the Datacontext for the view class ThisClassShouldBeTheDataContext

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;}
}

How can I bind Contact["John"].PhoneNumber to a textbox?

<TextBox Text=开发者_开发技巧"{Binding ?????}" />


The indexer notation is basically the same as C#:

<TextBox Text="{Binding Contacts[John].PhoneNumber}" />

See Binding Declarations Overview > Binding Path Syntax in MSDN for more info.

This won't, of course, work for arbitrary data types...

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号