我有一个列表框,其数据模板中包含一个文本框和一个组合框:
<ListBox Height="147" Margin="158,29,170,0" Name="PitcherListBox" VerticalAlignment="Top" ItemsSource="{Binding SomeCollectionOfObjects}" Background="Black">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBox Text="{Binding Path=Name}" />
<ComboBox ItemsSource="{Binding LocalArrayOfIntsProperty}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
我想将列表框绑定(bind)到对象集合(我已经成功完成),但我希望上述数据模板中的组合框将其 itemssource 设置为窗口上的本地属性(整数数组)。我仍然希望组合框在其所选项目和对象集合上的属性之间具有双向绑定(bind)...
我的代码如下: PitcherListBox.DataContext = this;
基本上最后,我希望列表框中的组合框具有与列表框本身不同的项目源。我似乎不知道如何在 XAML 中更改 ComboBox 的 ItemsSource。有人可以给我一些反馈吗?谢谢!
最佳答案
试试这个:
<ComboBox ItemsSource="{Binding LocalArrayOfIntsProperty, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type YourWindowTypeHere}}}" />
请注意,您需要将 YourWindowTypeHere
替换为包含 LocalArrayOfIntsProperty
的窗口类型!另请记住,您需要为该类型定义一个 xml 命名空间!
关于wpf - 将 WPF ComboBox 绑定(bind)到 ListBox DataTemplate 中的不同 ItemsSource,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2695954/