【问题标题】:WPF How to bind to a specific element in the CollectionWPF如何绑定到集合中的特定元素
【发布时间】:2010-05-18 06:25:32
【问题描述】:

我想绑定到集合中的特定元素。 但我不知道如何编写绑定。 这是代码:

public class MySource
{
 ..
 public string SomeProp;
 public ICollection<T> MyCollection;
 ..
}

this.DataContext = new MySource();

<TextBox Text={Binding SomeProp} />

<TextBox Text={Binding FIRST_ELEMENT_OF_THE_MyCollection} />
<TextBox Text={Binding SECOND_ELEMENT_OF_THE_MyCollection} />
<!--Ignore other elements-->

请尝试替换那些绑定字符串

谢谢

【问题讨论】:

    标签: wpf data-binding collections element


    【解决方案1】:
    <TextBox Text="{Binding MyCollection[0]}" /> 
    <TextBox Text="{Binding MyCollection[1]}" />
    

    【讨论】:

    • 如果您需要元素内部的属性,您可以这样写:{Binding MyCollection[0].Property}?
    • 是的。它应该在 WPF 中工作。如果没有,您可以在 ViewModel 中使用两个属性。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多