你怎么能索引属性绑定到WPF控件

你怎么能索引属性绑定到WPF控件

本文介绍了你怎么能索引属性绑定到WPF控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于该类ThisClassShouldBeTheDataContext作为DataContext的实例的视图

类ThisClassShouldBeTheDataContext
{
  公开联系方式联系方式{获取;设置;}
}类联系人
{
  公共IEnumerable的<&人GT;人{获取;设置;}
  公众人物这个[字符串名称]
  {
    得到
    {
      人员那里i.Name =名称选择我变种P =从我;
      返回p.First();
    }
  }
}类Person
{
  公共字符串名称{;设置;}
  公共字符串******中国{获取;设置;}
}

如何绑定联系[约翰。******中国来一个文本框?

 <文本框的文本={结合?????}/>


解决方案

该索引符号是基本相同的C#:

 <文本框的文本={绑定联系人[约翰] .PhoneNumber}/>

请参阅获得更多信息。

这不会,当然,对于任意的数据类型的工作......

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...

这篇关于你怎么能索引属性绑定到WPF控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 21:56