views:

69

answers:

1

The following code is not calling the SelectedItem's property setter in my view model.

<ComboBox x:Name="cmbGuaranteeType"  Margin="5,5,5,5" MinWidth="80" 
          ItemsSource="{Binding Source={StaticResource guaranteeTypesKey}}" 
          SelectedItem="{Binding RelativeSource={RelativeSource AncestorType={x:Type wpfToolkit:DataGridRow}}, Path=DataContext.GuaranteeType, Mode=TwoWay}"
      />

The view is a UserControl bound to a ViewModel. The UserControl has a WpfToolkit datagrid which is bound to an ObservableCollection on the ViewModel. The ObservableCollection consists of a List<T> items. The above combobox is binding to the "GuaranteeType" property on one of the T items as follows:

public GuaranteeType? GuaranteeType
{
    get { return _guaranteeType; }
    set { _guaranteeType = value; NotifyPropertyChanged(ConstGuaranteeType); }
}

When the UserControl is loaded, the GuaranteeType property's getter is called and the SelectedItem is set correctly. However, when I click on the ComboBox and attempted to change the SelectedItem, the setter for the GuarenteeType property is never called.

Additionally, I implemented the SelectionChanged="cmbGuaranteeType_SelectionChanged" in XAML against the View's codebehind and when the view is loading the SelectionChanged method is called once but it is not called when I release the mouse when trying to change the selected item of the combobox.

A: 

I apologize for such a vague answer, but I've seen this happen before and I seem to remember it having to do with the click event of the grid unbinding the SelectedItem. Similar things happen when binding radio buttons.

For combo boxes I essentially always default to using a ListCollectionView to expose the underlying observable collection and set the XAML like so:

<ComboBox ItemsSource="{Binding ListCollectionViewPropertyHere}" IsSynchronizedWithCurrentItem="True"/>

Then from code you can grab the ListCollectionViewPropertyHere.CurrentItem if you are polling or subscribe to the ListCollectionViewPropertyHere.CurrentItemChanged event if you need to be notified on click.

astonish
Scott Simock