Issue Summary
When using 2 cascading comboboxes, the child combobox does not select the current item, instead an empty item (I don't know where it comes from!) gets selected.
Issue Details
I have two comboboxes declared in xaml as given below. The two entities in play are StandardRack and RelayConfig
XAML:
<ComboBox ItemsSource="{Binding StandardRacks}" DisplayMemberPath="Name"
SelectedItem="{Binding StandardRack, Mode=TwoWay}" SelectedValuePath="Id"
<ComboBox ItemsSource="{Binding RelayConfigs}" DisplayMemberPath="DisplayName"
SelectedValue="{Binding DefaultRelayConfig, Mode=TwoWay}" SelectedValuePath="Id"
Here are the backing properties, and code to load comboboxes in ViewModel
ViewModel
private ObservableCollection<StandardRack> _standardRacks;
public ObservableCollection<StandardRack> StandardRacks {
get { return _standardRacks; }
set { _standardRacks = value; RaisePropertyChanged(() => StandardRacks); }
}
private StandardRack _standardRack;
public StandardRack StandardRack {
get { return _standardRack; }
set {
if (_standardRack != value) {
_standardRack = value;
LoadRelayConfigs();
RaisePropertyChanged(() => StandardRack);
}
}
}
private ObservableCollection<RelayConfig> _relayConfigs;
public ObservableCollection<RelayConfig> RelayConfigs {
get { return _relayConfigs; }
set { _relayConfigs = value; RaisePropertyChanged(() => RelayConfigs); }
}
private RelayConfig _defaultRelayConfig;
public RelayConfig DefaultRelayConfig {
get { return _defaultRelayConfig; }
set { _defaultRelayConfig = value; RaisePropertyChanged(() => DefaultRelayConfig); }
}
private void LoadRack() {
StandardRacks = new ObservableCollection<StandardRack>(
unitOfWork.StandardRackRepository.GetQueryable().Include(sr => sr.StandardRelay).ToList());
if (StandardRacks.Count > 0) {
StandardRack = Rack.StandardRack; //Set the current value of StandardRacks combobox
}
}
//Loads RelayConfigs Combobox based on Current Value of StandardRacks Combobox
private void LoadRelayConfigs() {
RelayConfigs = new ObservableCollection<RelayConfig>(
unitOfWork.RelayConfigRepository.GetQueryable()
.Where(rc => rc.StandardRelays.Any(srl => srl.Id == StandardRack.StandardRelay.Id)).ToList());
DefaultRelayConfig = Rack.DefaultRelayConfig; //Set Current Value of RelayConfigs Combobox. Does not work.
}
The above code loads both the comboboxes (StandardRacks and RelayConfigs) items properly. However the RelayConfigs selected value is not set to the one it is pointing to from XAML. Instead I get an empty item in the RelayConfigs Combobox as the current item.