【问题标题】:Windows Phone app: Binding data in a UserControl which is contained in a ListBoxWindows Phone 应用程序:在包含在 ListBox 中的 UserControl 中绑定数据
【发布时间】:2012-10-08 04:28:45
【问题描述】:

我有一个“AddressListBox”列表框,其中包含“AddressDetails”用户控件项,如下面的 .xaml 文件提取所示。 Addresses 集合定义为

ObservableCollection地址

和 Street、Number、PostCode、City 是 Address 类的属性。当我使用如下所示的“{Binding property}”语法时,绑定失败。当我在注释掉的代码中使用“虚拟”字符串时,绑定成功。我也尝试过“{Binding Path=property}”语法但没有成功。你能建议我应该使用什么语法来绑定用户控件中的数据吗?

        <ListBox x:Name="AddressListBox"

                 DataContext="{StaticResource dataSettings}" ItemsSource="{Binding Path=Addresses, Mode=TwoWay}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <!--
                    <usercontrols:AddressDetails AddressRoad="dummy" AddressNumber="dummy2" AddressPostCode="dummy3" AddressCity="dummy4">
                    </usercontrols:AddressDetails>
                    -->

                    <usercontrols:AddressDetails AddressRoad="{Binding Street}" AddressNumber="{Binding Number}" AddressPostCode="{Binding PostCode}" AddressCity="{Binding City}">
                    </usercontrols:AddressDetails>

                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

【问题讨论】:

    标签: data-binding user-controls listbox windows-phone


    【解决方案1】:

    您需要将用户控件上的数据上下文设置为这样的项目。

    <usercontrols:AddressDetails DataContext={Binding} ... </usercontrols:AddressDetails>

    现在您的其他绑定应该可以正常工作,因为它们是相对于项目而言的。

    【讨论】:

      猜你喜欢
      • 2013-03-26
      • 1970-01-01
      • 1970-01-01
      • 2015-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-22
      • 1970-01-01
      相关资源
      最近更新 更多