0

wpf/vmmv から始めたばかりです。コレクションをリスト ボックスにバインドする例を見てきました。例: xaml 、コード ビハインド (ページなど) で "DataContext = collection.."。

ビュー モデルには、ビューにバインドする必要がある 1 つのコレクションだけでなく、より多くのプロパティがあります。したがって、ビュー モデルをビューの 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 に答える