组合框选择在WPF中触发另一个组合框选择等

时间:2017-02-07 23:08:46

标签: c# wpf xaml mvvm combobox

我知道以前曾经问过这个问题,但出于某些原因,它似乎只对我有所帮助。我像这样设置我的XAML:

    <Grid>
        <ComboBox x:Name="cbCategories" ItemsSource="{Binding Categories}" DisplayMemberPath="Name" SelectedIndex="{Binding SelectedCategoryIndex}"/>
        <ComboBox x:Name="cbSourceParam" ItemsSource="{Binding SourceParameters}" DisplayMemberPath="Name" SelectedIndex="{Binding SelectedSourceParameterIndex}"/>
        <ComboBox x:Name="cbTargetParam" ItemsSource="{Binding TargetParameters}" DisplayMemberPath="Name" SelectedIndex="{Binding SelectedTargetParameterIndex}"/>
    </Grid>

然后我的ViewModel就像这样:

public class pmCopyParamToParamViewModel : ViewModelBase
    {
        public pmModel model;
        public ObservableCollection<CategoryWrapper> Categories { get; private set; }

        public pmCopyParamToParamViewModel(Document doc)
        {
            this.model = new pmModel(doc);
            this.Categories = model.CollectCategories();
            SelectedCategoryIndex = 0;
        }

        private ObservableCollection<ParameterWrapper> _sourceParameters;
        public ObservableCollection<ParameterWrapper> SourceParameters
        {
            get { return _sourceParameters; }
            set
            {
                if (_sourceParameters == value) return;

                _sourceParameters = value;
                RaisePropertyChanged(() => SourceParameters);
                SelectedSourceParameterIndex = 0;
            }
        }

        private ObservableCollection<ParameterWrapper> _targetParameters;
        public ObservableCollection<ParameterWrapper> TargetParameters
        {
            get { return _targetParameters; }
            set
            {
                if (_targetParameters == value) return;

                _targetParameters = value;
                RaisePropertyChanged(() => TargetParameters);
                SelectedTargetParameterIndex = 0;
            }
        }

        // logic for selected category index
        private int _selectedCategoryIndex;
        public int SelectedCategoryIndex
        {
            get { return _selectedCategoryIndex; }
            set
            {
                if (_selectedCategoryIndex == value) return;

                _selectedCategoryIndex = value;
                RaisePropertyChanged(() => SelectedCategoryIndex);
                SourceParameters = model.CollectParameters(Categories[SelectedCategoryIndex].ID, new string[] { "String", "Double", "Integer" });
            }
        }

        private int _selectedSourceParameterIndex;
        public int SelectedSourceParameterIndex
        {
            get { return _selectedSourceParameterIndex; }
            set
            {
                if (_selectedSourceParameterIndex == value) return;

                _selectedSourceParameterIndex = value;
                RaisePropertyChanged(() => SelectedSourceParameterIndex);
                TargetParameters = model.CollectParameters(Categories[SelectedCategoryIndex].ID, new string[] { SourceParameters[SelectedSourceParameterIndex].StorageType });
            }
        }

        private int _selectedTargetParameterIndex;
        public int SelectedTargetParameterIndex
        {
            get { return _selectedTargetParameterIndex; }
            set
            {
                if (_selectedTargetParameterIndex == value) return;

                _selectedTargetParameterIndex = value;
                RaisePropertyChanged(() => SelectedTargetParameterIndex);
            }
        }
    }

我期望发生的是,在ViewModel启动时,它将收集所有类别。然后我调用SelectedCategoryIndex并将其设置为0.反过来,应该触发SourceParameters更新并将所选项目最初设置为0.这反过来会触发TargetParameters触发并设置初始SelectedParameterIndex为0.

到目前为止,我只看到类别和源参数设置,但是直到我手动触摸/更改源参数组合框的选择时,目标参数组合框才会设置。

我在这里错过了什么吗?谢谢!

1 个答案:

答案 0 :(得分:1)

这可能是问题吗?

private int _selectedSourceParameterIndex; // Starts off as 0
public int SelectedSourceParameterIndex
{
    set
    {
        // Setting to zero would not change the value, and this will return
        if (_selectedSourceParameterIndex == value) return;

        //... nothing here gets executed ...
        _selectedSourceParameterIndex = value;
        RaisePropertyChanged(() => SelectedSourceParameterIndex);
        TargetParameters = model.CollectParameters(Categories[SelectedCategoryIndex].ID, new string[] { SourceParameters[SelectedSourceParameterIndex].StorageType });
    }
}

我个人更喜欢绑定SelectedItem而不是SelectedIndex。它将为您提供实际对象(如果没有选择,则为null),因此您不必处理组合框/列表索引的复杂性。