【问题标题】:How to bind individual collection items to different text boxes in WPF如何将单个集合项绑定到 WPF 中的不同文本框
【发布时间】:2010-11-17 23:16:14
【问题描述】:

假设我有一组电话号码:

phonenumbers.add(new phonenumber("mobile", "1234"));
phonenumbers.add(new phonenumber("home", "5678"));

如果我有 2 个文本框,txtMobile 和 txtHome,如何将每个电话号码绑定到各个文本框?

谢谢!

【问题讨论】:

    标签: .net c#-4.0 binding .net-4.0 wpf-4.0


    【解决方案1】:

    您可以在 WPF 中索引您的绑定。试试这个:

    <TextBox Text="{Binding phonenumbers[0]}" />
    <TextBox Text="{Binding phonenumbers[1]}" />
    

    【讨论】:

    • 谢谢。我的来源实际上是一个 LINQ 类 Member.PhoneNumbers。每个成员最多可以有 3 种电话号码类型。我希望能够将每个文本框绑定到每种类型的电话号码?类似 Text="{Binding Member.PhoneNumbers["Mobile"]}" 的东西。是否可以?还有其他方法吗?
    • 相信索引器可以是任何类型的。试试 Text="{Binding Member.PhoneNumbers['Mobile']}"
    • 我试过了。不幸的是,它不起作用。希望您有其他建议。
    • 没有?您的 PhoneNumbers 集合是类的属性而不是字段吗?你还记得单引号吗?
    【解决方案2】:

    您可以绑定到索引。

    Text={Binding phonenumbers[0]}
    

    否则为电话号码创建单独的属性并绑定到它们。

    【讨论】:

    • 谢谢!请参阅我对马特汉密尔顿的评论。我希望你能帮助我。
    猜你喜欢
    • 2015-01-20
    • 2011-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多