List<Item> Items
をカスタムオブジェクトとして表示するListBox
があります。 ForeachアイテムがList<string> Options
であることをユーザーに見せたいと思っています。選択したアイテムがアイテムのプロパティに関連付けられています。リストボックスでは、個々のItemプロパティをバインドするのに問題はありませんが、オプションのリストを取得するためにDataContextにバックアップする方法はありますか?DataContext ComboBox ListBox内でのバインド
ビューモデルは、私はできるだけ多くの余分なコードを切り出して読みやすい例を取得しようとした
<ListBox x:Name="ItemsListBox" ItemsSource="{Binding Items}">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Height="50" >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBlock x:Name="ItemProperty1TB"
Text="{Binding Path=Property1, Mode=OneWay}"
Grid.Column="0"
/>
<ComboBox x:Name="OptionsCB"
SelectedItem ="{Binding ChosenOptions, Mode=TwoWay}"
ItemsSource="{Binding Path=DataContext.Options}"
Grid.Column="1"
PlaceholderText="Option"
/>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
ページのDataContextの
class ViewModel
{
public ObservableCollection<string> Options { get; set; }
public ObservableCollection<Item> Items { get; set; }
}
XAMLとして設定されています。
How to bind to a source inside a ListBox different from the ItemsSource already specifiedこれは存在しないAncestorTypeを使用していますか?
ComboBox inside Listbox.ItemTemplate Binding problemこれは静的リソースにバインドされます。オプションを静的リソースに入れる必要がありますか?
ElementName
は有望に見えるが、私のIDEは要素だけがリストボックス内にスコープをお勧めします... はVISUAL STUDIO
を信用していない私は、このすべて間違っについては行くだろうか?
ViewModelには、プロパティではなく、フィールドを持っている必要があります。それは実際のコードですか? –
これは実際のコードではありません。私はすぐにそれを変更します – JaredStroeb
'ItemsSource =" {バインディングパス= DataContext.Options、ElementName = ItemsListBox} "を試してください" –