C# で MVVM に従っており、リストボックスにビューを表示しようとしています。
リストボックスの itemsource を (viewmodels コレクションをバインドして使用するのではなく、コードで) 設定し、データテンプレートを xaml のビューに設定します。私が遭遇している問題は、ビューからデータコンテキストを削除すると、ビューが常にデフォルトのコンストラクター値でロードされることですが、正常にロードされます。
以下は、私がxamlで作成しているリストボックスです
<ListBox Name="lbCatalogues" HorizontalContentAlignment="Stretch">
<ListBox.ItemTemplate>
<DataTemplate>
<view:CatalogueRowView/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
これは私のビューの xaml です。DataContext を削除すると動作します
<UserControl.DataContext>
<model:CatalogueModel />
</UserControl.DataContext>
<Grid Name="Container" >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="50" />
<ColumnDefinition Width="100" />
<ColumnDefinition Width="100" />
<ColumnDefinition Width="1*" />
<ColumnDefinition Width="40" />
</Grid.ColumnDefinitions>
<!-- Catalogue_ID, UploadedTime, Client_ID, Name, Desc, Filesize -->
<Label Name="lblCatalogueID" Content="{Binding Path=CatalogueID}" Grid.Column="0"/>
<Label Name="lblUploadedTime" Content="{Binding Path=UploadedTime}" Grid.Column="1"/>
<Label Name="lblCatalogueName" Content="{Binding Path=Name}" Grid.Column="2"/>
<Label Name="lblCatalogueDescription" Content="{Binding Path=Description}" Grid.Column="3"/>
<Label Name="lblFilesize" Content="{Binding Path=Filesize}" Grid.Column="4"/>
<Grid/>
これは、リストボックス ItemSource を設定しているコードです。
lbCatalogues.ItemsSource = catalogueViewModel.Records;
私の質問は、リストボックス内の各項目がそのリストボックス Itemsource にリンクされた DataContext を持つように、ビューをリストボックス内に正しくロードするにはどうすればよいですか?