我班上有这两个名单:
public ObservableCollection<string> List1{ get; set; }
public ObservableCollection<string> List2{ get; set; }
我想将它们绑定到此列表框:
<ListBox ItemsSource="{Binding List1}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding}" Margin="0,0,0,6" />
<ComboBox Width="70" ItemsSource="{Binding List2} SelectedIndex="0" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
以某种方式,每个项目的文本块将是List1的项目,每个组合框将具有List2项目作为选项。我不想创建二级关系,因为List2的项目会经常更改,我不想单独更新每个列表。
答案 0 :(得分:0)
您可以使用相对绑定:
<ComboBox Width="70" ItemsSource="{Binding DataContext.List2,RelativeSource={RelativeSource AncestorType=ListBox}}" SelectedIndex="0" />