0

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.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Jatin
  • 4,023
  • 10
  • 60
  • 107

1 Answers1

0

Is the 'DefaultRelayConfig' part of the 'RelayConfigs' selection?

  • Actually, it is not. Its loaded in the previous Unit Of Work. And that seems to be the issue. Well, but the SelectedValuePath="Id" should take care of that ? – Jatin May 25 '12 at 12:37
  • Diego, DefaultRelayConfig is not the part of the RelayConfigs collection (you asked for selection, I am confused by what you meant). But the DefaultRelayConfig.Id and the RelayConfigs RelayConfig.Id are same so, I was hoping SelectedValuePath="Id" should select the correct RelayConfig from RelayConfigs collection. – Jatin May 25 '12 at 13:06
  • @DiegoModoloRiberio - asking for clarification should be in a comment, not an answer. – Wonko the Sane May 25 '12 at 15:21
  • Sorry @WonkotheSane. I'll not do that again. – Diego Modolo Ribeiro May 25 '12 at 20:18
  • @Nirvan, I think the problem is that. Could you post a sample of your project so I could take a better look at it? – Diego Modolo Ribeiro May 25 '12 at 20:20
  • @DiegoModoloRibeiro: Not a problem - just a clarification of how things are usually done 'round here. Please keep contributing! – Wonko the Sane May 29 '12 at 12:42