0

问题摘要

当使用 2 个级联组合框时,子组合框不会选择当前项目,而是选择一个空项目(我不知道它来自哪里!)。

问题详情

我在 xaml 中声明了两个组合框,如下所示。发挥作用的两个实体是StandardRackRelayConfig

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"

这是支持属性,以及在 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.
}

上面的代码正确加载了两个组合框(StandardRacks 和 RelayConfigs)项。但是,RelayConfigs 选择的值未设置为它从 XAML 指向的值。相反,我在 RelayConfigs 组合框中得到一个空项目作为当前项目。

4

1 回答 1

0

'RelayConfigs' 选择中的'DefaultRelayConfig' 部分吗?

于 2012-05-25T12:33:35.270 回答