简体   繁体   中英

WPF Databound ComboBox unexpected behavior

I was just put on a project using wpf and data binding. I've got a ComboBox that first reads it's selected value from the view model and then immediately writes the default (incorrect) value back to the view model. The call stack says that InitializeComponent() is the origin for both the get and set calls. There are a dozen or so other controls that don't behave like this. Is there some property I'm missing that would prevent that first incorrectly set value?

<ComboBox Grid.Column="1" Grid.Row="2" 
      Name="traceFileOptions"  ItemsSource="{Binding TraceFileOptionsCollection}"  
      SelectedValue="{Binding FileOption}"  DisplayMemberPath="Text" SelectedValuePath="Value" 
      IsEnabled="{Binding WriteToFile}" SelectedIndex="{Binding FileTraceComboBoxDefault}" />

It looks like it's doing exactly what you asked it to:

first,

SelectedValue="{Binding FileOption}"

(and, this is a binding, so subsequently setting the selection will update the property in the viewmodel)

then,

SelectedIndex="{Binding FileTraceComboBoxDefault}" 

You probably want to set/bind just one of SelectedValue or SelectedIndex.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM