简体   繁体   中英

WPF ComboBox How to bind the selected item in code?

I have a combo box which is populated on selection's from two other combo'. No problem I have this working. Problem is I only want to activate the Selected Item binding after this has happened.

Here is my combo

 <cuc:ComboBox Name="GopLenTypeCombo" Width="240" Height="24"
                      IsSynchronizedWithCurrentItem="True"
                      SelectedValue="{Binding Mode=TwoWay, Source={StaticResource ProfileDataProvider}, XPath=GopLenType}" 
                      IsEnabled="False"/>

How do I stop the SelectedValue binding working untill I have populated the combo ?

I don't know whether this is possible in pure XAML, but if you have an underlying ViewModel, this wouldn't be too hard:

What you would need to do is to bind your third ComboBox to a property on the ViewModel (let's call this property Combo3Items).

You would also need to bind the two first ComboBoxes to properties on the same ViewModel (let's call these Combo1Items and Combo2Items, respectively). In addition to that, you could bind both of these ComboBoxes' SelectedValue to properties on the ViewModel, so that the ViewModel knows which items are selected.

Initially, Combo3Items would be empty, but as soon as the two bound SelectedValues are properly assigned, you can fill Combo3Items and raise an INotifyPropertyChanged event.

The MVVM architecture is very powerful. If you don't already know it, you can read more about it here .

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