locked
ComboBox SelectedItem RRS feed

  • Question

  • The item is not changing for my ComboBox when I bind to it.

    XAML:

    <ComboBox Grid.Column="1" Grid.Row="3" SelectedItem="{Binding State, ElementName=ctlAddress}">

    <ComboBoxItem>Colorado</ComboBoxItem>

    </ComboBox>

    Public Shared ReadOnly StateProperty As DependencyProperty = DependencyProperty.Register("State", GetType(String), GetType(Address))

    <Category("Common Properties")> _

    Public Property State() As String

    Get

    Return CType(GetValue(StateProperty), String)

    End Get

    Set(ByVal value As String)

    SetValue(StateProperty, value)

    End Set

    End Property

    The value doesn't change when I set the State property. Am I missing something. My textboxes are getting set.

    Monday, June 23, 2008 7:46 PM

Answers

  • The Text property binds two way by default, but SelectedItem only binds one way, i.e. it is the source of the binding by default. If you really want to use SelectedItem, you can explicitly set Mode=TwoWay, but you need to use a converter for it to work properly.

    • Marked as answer by Marco Zhou Monday, June 30, 2008 9:55 AM
    Sunday, June 29, 2008 11:07 PM

All replies

  • Hi, I think it's better to bind to the Text property of the ComboBox, and use a converter if necessray.

    Hth,

    Mark
    Wednesday, June 25, 2008 10:10 PM
  • That works. Why does setting the text property work and not SelectedItem?
    Sunday, June 29, 2008 3:03 PM
  • The Text property binds two way by default, but SelectedItem only binds one way, i.e. it is the source of the binding by default. If you really want to use SelectedItem, you can explicitly set Mode=TwoWay, but you need to use a converter for it to work properly.

    • Marked as answer by Marco Zhou Monday, June 30, 2008 9:55 AM
    Sunday, June 29, 2008 11:07 PM