0

我只是从 wpf/vmmv 开始。我见过将集合绑定到列表框的示例。示例:在 xaml 中,在代码隐藏(例如页面)“DataContext = collection..”中。

我的视图模型具有更多的属性,而不仅仅是需要绑定到视图的单个集合。因此,我想将视图模型设置为视图的 DataContext,然后在 xaml 中将视图模型的集合绑定到 ListBox。假设我的视图模型设置为 DataContext 并且它有一个名为“Customers”的属性,那么将属性绑定到 xaml 中的 ListBox 的正确方法是什么?我试过了,但它不起作用。

谢谢。

4

2 回答 2

2

你的意思是'如何将集合绑定到'ListBox'?你会这样做:

<ListBox ItemsSource="{Binding Customers}" />

或这个:

<ListBox ItemsSource="{Binding Path=Customers}" />

如果要绑定Customer类的每个实例的内部值,可以执行以下操作:

<ListBox ItemsSource="{Binding Customers}" >
    <ListBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Name}" />
            <TextBlock Text="{Binding Age}" />
            <TextBlock Text="{Binding EyeColour}" />
        </DataTemplate>
    </ListBox.ItemTemplate>        
</ListBox>
于 2013-08-23T13:01:51.937 回答
0

我猜你想显示属性“Customers”,你要做的是定义ListBox的ItemTemplate,在ItemTemplate里面定义DataTemplate,然后将Customers绑定到一个控件,如下所示:

<ListBox ItemsSource="{Binding}" >
    <ListBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Path=Customers}"/>
            ......something else you want display
        </DataTemplate>
    </ListBox.ItemTemplate>

</ListBox>
于 2013-08-23T12:45:17.070 回答