locked
ComboBox SelectedValuePath and SelectedValue RRS feed

  • Question

  • Hi All.

    Can someone detail explain about ComboBox binding options SelectedValuePath and SelectedValue.

    For instance:

    the Field1_Id is ID field of ComboBox source;
    the Field2 is fiels that I would like to display in ComboBox;
    the Field3 is field that I would like to save selected ComboBox value in DB table.

    How code will in XAML and in VB based on those fields to save Field3 to DB table.

    Thanks.

    Tuesday, March 6, 2012 9:02 PM

Answers

  • Can't say without seeing a fuller code example, but no it doesn't.

    "the Field1_Id is ID field of ComboBox source;" I don't understand. The ComboBox ItemsSource is the database set that defines the options in the combobox. SelectedValue binds to another object OUTSIDE of the options collection, so needs to have a RelativeSource or ElementName to help the binding get outside of the current DataContext (the options collection)

    <ComboBox Name="ComboBox1" Grid.Column="1"      
    SelectedValue="{Binding Path=DataContext.CollectionViewTable2.CurrentItem.Field3, RelativeSource={RelativeSource AncestorType=Window}}"
    DisplayMemberPath="Field2"
    SelectedValuePath="Field1" />
    This is just an example that assumes your CollectionViewTable2 is a property of a ViewModel which is the DataContext of a Window.
    Without seeing your implementation we cannot say what it should be.

    I don't believe you should you be setting SelectedIndex to -1 and IsSynchronizedWithCurrentItem=true. 

    If you make the SelectedValue binding Mode=TwoWay, it should handle that for you. Hard coding index to -1 simply removes any value that may have initially come in, if two-way binding is your aim?

    If you are binding something to SelectedValue/SelectedItem then the source data can be TwoWay bound depending on the rest of your code and the way that you bind the database data. So you shouldn;t even need to do any manual data manipulation in code.

    Your best bet may simply be to search this forum for similar examples.
    There are many, depending on your specific datacentric implementation (ADO, Entity Framework, etc).

    Good luck,
    Pete


    #PEJL

    • Marked as answer by Min Zhu Wednesday, March 14, 2012 1:37 AM
    Tuesday, March 6, 2012 11:40 PM
  • Hi eugzl,

    Just checking in to see if the information was helpful. Please let us know if you would like further assistance.

    Have a great day!


    Min Zhu [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by zleug Tuesday, March 13, 2012 2:41 PM
    Monday, March 12, 2012 2:31 AM

All replies

  • SelectedValue is the value defined by SelectedValuePath

    So in your case, DisplayMemberPath=Field2 SelectedValuePath=Field3, SelectedValue is the BINDING to your DB table or class.

      

    <ComboBox ItemsSource="{Binding MyOptions}" DisplayMemberPath="Field2" SelectedValuePath="Field3"  
                       SelectedValue="{Binding MyData.SelectedVal, Mode=TwoWay}"></ComboBox>

      

    Also, read here : http://msdn.microsoft.com/en-us/library/ms788714.aspx - How to: Use SelectedValue, SelectedValuePath, and SelectedItem

    VB example here : http://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.selector.selectedvaluepath(v=vs.95).aspx#Y505

      

    Regards,
    Pete


    #PEJL




    Tuesday, March 6, 2012 9:49 PM
  • Hi XAML guy. Thanks for replay.

    OK. If source of ComboBox is Table1 with the fields Field1_Id and Field2. And Table2 is table where located Field3. So XAML of ComboBox will look like:

    <ComboBox Name="ComboBox1" Grid.Column="1"
    SelectedValue="{Binding Path=Field1_Id}"
    DisplayMemberPath="Field2"
    SelectedValuePath="Field1"
    IsSynchronizedWithCurrentItem="True" SelectedIndex="-1"
    </ComboBox>

    And in VB to save Field3:

    CollectionViewTable2.CurrentItem("Field3") = Me.ComboBox1.SelectedValue

    Is that correct?

    Thanks.





    • Edited by zleug Tuesday, March 6, 2012 10:25 PM
    Tuesday, March 6, 2012 10:02 PM
  • Can't say without seeing a fuller code example, but no it doesn't.

    "the Field1_Id is ID field of ComboBox source;" I don't understand. The ComboBox ItemsSource is the database set that defines the options in the combobox. SelectedValue binds to another object OUTSIDE of the options collection, so needs to have a RelativeSource or ElementName to help the binding get outside of the current DataContext (the options collection)

    <ComboBox Name="ComboBox1" Grid.Column="1"      
    SelectedValue="{Binding Path=DataContext.CollectionViewTable2.CurrentItem.Field3, RelativeSource={RelativeSource AncestorType=Window}}"
    DisplayMemberPath="Field2"
    SelectedValuePath="Field1" />
    This is just an example that assumes your CollectionViewTable2 is a property of a ViewModel which is the DataContext of a Window.
    Without seeing your implementation we cannot say what it should be.

    I don't believe you should you be setting SelectedIndex to -1 and IsSynchronizedWithCurrentItem=true. 

    If you make the SelectedValue binding Mode=TwoWay, it should handle that for you. Hard coding index to -1 simply removes any value that may have initially come in, if two-way binding is your aim?

    If you are binding something to SelectedValue/SelectedItem then the source data can be TwoWay bound depending on the rest of your code and the way that you bind the database data. So you shouldn;t even need to do any manual data manipulation in code.

    Your best bet may simply be to search this forum for similar examples.
    There are many, depending on your specific datacentric implementation (ADO, Entity Framework, etc).

    Good luck,
    Pete


    #PEJL

    • Marked as answer by Min Zhu Wednesday, March 14, 2012 1:37 AM
    Tuesday, March 6, 2012 11:40 PM
  • Totally agree with Pete.

    Min Zhu [MSFT]
    MSDN Community Support | Feedback to us

    Wednesday, March 7, 2012 2:54 AM
  • Hi eugzl,

    Just checking in to see if the information was helpful. Please let us know if you would like further assistance.

    Have a great day!


    Min Zhu [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by zleug Tuesday, March 13, 2012 2:41 PM
    Monday, March 12, 2012 2:31 AM