Search code examples
wpfxamlbindingcomboboxselectedvalue

Combobox's SelectedValue (or SelectedItem) OneWay binding not working. Any ideas?


In the below window, the Existing Reports combo is bound to an observeablecollection of reportObjects. I have a reportObject property currentReport bound to the combo's SelectedValue property, OneWay. However, that's not working when bound in XAML.

SelectedValue="{Binding currentReport, Mode=OneWay}"

TwoWay binds fine, but I can't do it that way without writing an undo() method to the reportObject class. I'm binding the currentReport's properties to the various textboxes for editing. I want to bind OneWay so the source doesn't get changed. The currentReport's properties are all TwoWay bound to the corresponding textboxes so when I update the table in SQL [Save], it'll pull from that object, who's data is current.

<TextBox Text="{Binding currentReport.reportName, Mode=TwoWay}"

All of the properties bound from currentReport to the textboxes work fine as well. The only problem is the OneWay binding from the SelectedValue to the currentReport object. Does anyone have any ideas how to get this to work? I saw there was a bug, but the post I saw was 2009.

enter image description here

Sorry about the yellow. Not my idea. =)

EDIT: Added this XAML just in case.

<ComboBox ItemsSource="{Binding reportsCollection}" SelectionChanged="cboReports_SelectionChanged"
                                  DisplayMemberPath="displayName"
                                  SelectedValue="{Binding currentReport, Mode=TwoWay}"
                                  x:Name="cboReports" Width="342" Height="40" VerticalAlignment="Center"/>  

Solution

  • Forget about you need to change values - that is a separate problem - need to review your data design. Start with the UI problem question. If you want a user to be able to select an item from a combo box then it must have two way binding. Your first question is SelectedValue="{Binding currentReport, Mode=OneWay}" is failing why?