Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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;
 }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top