我有一些問題,ComboBox
嵌套在ListBox
。我想添加相同的ItemsSource
(從數據庫中獲得,從代碼隱藏中添加)到在ListBox
中創建的comboboxes
,但不知道如何。任何想法如何做到這一點?如何添加ItemsSource到代碼隱藏嵌套在ListBox模板中的Combobox?
</Window.Resources>
<Style x:Key="lbxKey" TargetType="ListBox">
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<StackPanel/>
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
<Setter Property="ItemTemplate">
<Setter.Value>
<DataTemplate DataType="{x:Type Type1}">
<StackPanel Orientation="Horizontal" Width="Auto" HorizontalAlignment="Stretch">
<TextBlock TextTrimming="CharacterEllipsis" Width="200" Text="{Binding NAMETYPE1}" HorizontalAlignment="Left"></TextBlock>
<ComboBox HorizontalAlignment="Stretch" Tag="{Binding IDTYPE1}">
<ComboBox.ItemsSource>
<!-- no idea what should be here or even if this is needed -->
</ComboBox.ItemsSource>
<ComboBox.ItemTemplate>
<DataTemplate DataType="{x:Type Type2}">
<StackPanel Orientation="Horizontal" Width="100">
<TextBlock Text="{Binding NAMETYPE2}" TextTrimming="CharacterEllipsis"/>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</StackPanel>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
在代碼隱藏:
lbxListbox.ItemsSource = observableCollectionFromDatabase;
//here should be sth to set ItemsSource for comboboxes in ListBox
銻建議設置組合框'ItemsSource'爲'ListBox.DataContext'並設置項目以DataContext的,但我不知道爲什麼這個答案被刪除。 'ItemsSource =「{Binding RelativeSource = {RelativeSource FindAncestor,AncestorType = {x:Type ListBox}},Path = DataContext}」' – Kelk