Linking WPF ComboBox with another ItemsSource in ListBox DataTemplate

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>

      

I want to bind a list to a collection of objects (which I have done successfully), but I want the combo box in the above datatemplate to have its source items set to a local property on the window (ints array). I still want the combobox to have a two-way binding between the selected item and a property in the collection of objects ...

I have a code: PitcherListBox.DataContext = this;

Basically, in the end, I want the combo box to have a field with other items besides the list itself. I can't figure out how to change the ComboBox ItemsSource in XAML. Can anyone provide me with some feedback? Thanks!

+2


a source to share


1 answer


Try the following:

<ComboBox ItemsSource="{Binding LocalArrayOfIntsProperty, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type YourWindowTypeHere}}}" />

      



Note that you need to replace with the YourWindowTypeHere

type of the window containing LocalArrayOfIntsProperty

! Also remember that you will need to define an xml namespace for this type!

+4


a source







All Articles