繁体   English   中英

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

[英]combobox selection triggers another combobox selection etc. in WPF

我知道以前已经有人问过这个问题,但是由于某种原因,它似乎只能在某种程度上对我有用。 我这样设置我的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。

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

我在这里想念什么吗? 谢谢!

这可能是问题吗?

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),因此您不必处理组合框/列表索引的复杂性。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM