views:

819

answers:

2

I have a listbox with a datatemplate that holds a number of controls bound to my collection.

What i want is to change the itemsource for one of these comboboxes dependant upon the value selected in one of the other comboboxes in the same row. I don't want to change the itemsource for all corresponding comboboxes within the rest of the rows in the listbox.

How do I get a handle on the control in the selected row only.

Is this something that is easier to try doing witht the WPF datagrid?

Thanks.

A: 

Get the SelectionChanged event of that paticular combobox and set the Itemsource of the other combobox inside the event.

private void cmb1SelectionChanged(object sender, SelectionChangedEventArgs e)
{
  cmboBox2.ItemSource = yourItemSource;
}

Also it is better to get the SelectionChaged event of listview and handle it.

 private void OnlistviewSelectionChanged( object sender, SelectionChangedEventArgs e )
 {
    // Handles the selection changed event so that it will not reflect to other user controls.
    e.Handled = true;
 }
Sauron
+1  A: 

This is actually easier with the ListBox, as the DataTemplate defines all the controls for a row.

I think the easiest way is to use a converter on a binding. You will bind your second ComboBox's ItemsSource to the SelectedItem of the first ComboBox:

<myNamespace:MyConverter x:Key="sourceConverter" />

<StackPanel Orientation="Horizontal>
    <ComboBox x:Name="cbo1" ... />
    ...
    <ComboBox ItemsSource="{Binding SelectedItem, ElementName=cbo1, Converter={StaticResource sourceConverter}}" ... />
    ...
</StackPanel>

Note that if you need additional information from the DataContext of the Row, you can make it a MultiBinding and an IMultiValueConverter, and pass in the DataContext easily by doing:

<MultiBinding Converter="{StaticResource sourceConverter}">
    <Binding />
    <Binding Path="SelectedItem", ElementName="cbo1" />
</MultiBinding>

Then, in your converter class, do whatever it is you have to do in order to get the correct items source.

Abe Heidebrecht
Thanks very much for your reply. That's just what I was looking for. I'd completely overlooked converters - now I can think of so many possibilities as to where I could use them!