views:

299

answers:

1

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 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...

I have the following in code: PitcherListBox.DataContext = this;

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!

+1  A: 

Try this:

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

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!

gehho
I created a namespace "xmlns:Local="clr-namespace:MyApp" and used the line provided below, using this for the type: AncestorType={x:Type Local:DetailsWindow} and I get an error: Ancestor type must be specified for relative source in FindAncestor mode
Nevermind, I just had to build and the error went away. Thanks so much for your help...
Yeah, this is a bug in VS2008 (do not know about VS2010). As you said, it typically disappears after re-building. BTW: If my answer was helpful, you should mark it as accepted by clicking on the checkmark besides my answer.
gehho