views:

81

answers:

3

When binding a property to the 'SelectedItem' of a WPF Combobox, I would expect to see that property setter get called every time the combobox's selection is changed. I'm not seeing that. Should the Combobox be calling the bound 'SelectedItem's property setter when the selection is changed?

Addition: I actually have the binding partially there: the property getter gets called and the property setter gets called once when the combobox is first loaded/selected and never called again upon later selection changes.

One thing I noticed is that when I put the IsSynchronizedWithCurrentItem="True" in the combobox entry in Xaml, the setter gets called once upon combobox loading/initial selection, but never again. When I remove that combobox attribute, the setter never gets called. Very strange.

Also, i'm referring to a view model property, not a dependency property. At least I didn't set it up as a dependency property. I'm new to this (Surprise!), so any more nuggets of info regarding this subject would be most appreciated.

xaml Code:

<ComboBox MinWidth="300" Margin="5,0,0,5"
   ItemsSource="{Binding KeywordCollectionTypes, Mode=OneWay}"
   SelectedItem="{Binding KeywordCollectionType, Mode=TwoWay}"
   IsSynchronizedWithCurrentItem="True"/>

ViewModel Code (The Binded Attributes):

 public Collection<string> KeywordCollectionTypes
    {
        get
        {
            return _KeywordCollectionTypes;
        }
    }

public string KeywordCollectionType
    {
        get
        {
            return _KeywordCollectionType;
        }
        set
        {
            _KeywordCollectionType = value;

            OnPropertyChanged("KeywordCollectionType");
        }
    }

One more bit of info is that the combobox is within a DataGrid.RowDetailsTemplate, so could this strange update behavior be related to it being within a row details?

A: 

this is how you bind a combobox's selected item to your data model....

 <ComboBox SelectedItem="{Binding Path=MyValue}"/>

where MyValue is a property of your DataContext/Data Model.

if you want to bind a different control, such as TextBlock, to the selected item, here is a small example...

<ComboBox Name="myComboBox" SelectedIndex="0">
   <ComboBoxItem>1</ComboBoxItem>
   <ComboBoxItem>2</ComboBoxItem>
   <ComboBoxItem>3</ComboBoxItem>
</ComboBox>
<TextBlock Text="{Binding ElementName=myComboBox, Path=SelectedItem.Content}"/>

here, the text block will update whenever the ComboBox selection changes.

Muad'Dib
+1  A: 

You're doing something wrong. From a current project of mine:

<ComboBox
   ItemsSource="{Binding Configurations}" 
   SelectedItem="{Binding SelectedConfiguration, Mode=TwoWay}"/>

The SelectedConfiguration property setter gets called every time the selected item changes.

Edit

I'm assuming that your object isn't a DependencyObject, and that the property you're binding to isn't a dependency property. If it is a dependency property, then as DK observes, binding will update the property value by calling SetValue and will bypass the CLR property accessor; if you want to insert logic into that control flow, reference a callback method when you register the dependency property.

Robert Rossney
since my answer was about DP, I'll remove it
DK
A: 

I finally figured out the problem I was having. in the binding statement to the combobox's SelectedItem, I needed to put: "UpdateSourceTrigger=PropertyChanged"

Like this:

<ComboBox MinWidth="300" Margin="5,0,0,5"
                                      ItemsSource="{Binding KeywordCollectionTypes, Mode=OneWay}"
                                      SelectedItem="{Binding KeywordCollectionType, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>

Here's the thread that got me to the fix:

http://stackoverflow.com/questions/3214572/

BrianP