问题描述
我有一个ListBox,在其数据表中包含一个文本框和一个组合框:
I have a ListBox that contains a textbox and a combobox in its datatemplate:
<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>
我想将列表框绑定到一个对象集合我想要上面的datatemplate中的组合框将它的itemssource设置为窗口(ints数组)的本地属性。我仍然希望组合框在选择的项目和对象集合之间的一个属性之间有一个双向绑定...
I want to bind the listbox to a collection of objects (which I've done successfully), but I want the combobox in the above datatemplate to have its itemssource set to a local property on the window (array of ints). I still want the combobox to have a two-way bind between its selected item and a property on the collection of objects...
我有以下代码:
PitcherListBox.DataContext = this;
I have the following in code:PitcherListBox.DataContext = this;
基本上最后,我想在列表框中的组合框有一个不同的itemssource比列表框本身。我似乎不知道如何改变XAML中的ComboBox的ItemsSource。有人可以给我一些反馈吗?谢谢!
Basically in the end, I want the combobox within the listbox to have a different itemssource than the listbox itself. I can't seem to figure out how to change the ComboBox's ItemsSource in XAML. Can someone provide me some feedback? Thanks!
推荐答案
尝试:
<ComboBox ItemsSource="{Binding LocalArrayOfIntsProperty, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type YourWindowTypeHere}}}" />
请注意,您需要替换 YourWindowTypeHere
包含 LocalArrayOfIntsProperty
!的窗口类型。还要记住,您将需要为该类型定义一个xml命名空间!
Note that you need to replace YourWindowTypeHere
with the type of the Window containing the LocalArrayOfIntsProperty
! Also remember that you will need to define an xml namespace for that type!
这篇关于将WPF ComboBox绑定到ListBox DataTemplate中的不同ItemsSource的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!