MVVM TabControl更新ComboBox

时间:2013-06-21 16:45:45

标签: c# wpf mvvm mvvm-light

作为一个概念,我对MVVM很新,我正在尝试进行设置,以便更改TabControl的选定索引将改变我所拥有的ComboBox的项目源。目前我的设置如下:

    public int SelectedTabIndex
    {
        get
        {
            return _selectedTabIndex;
        }
        set
        {
            _selectedTabIndex = value;
            if (_selectedTabIndex == 0)
            {
                _readOnlyArray = ReadOnlyArrays.ReadOnlyColumnArrays.LoanerItemsSelect;
            }
            else if (_selectedTabIndex == 1)
            {
                _readOnlyArray = ReadOnlyArrays.ReadOnlyColumnArrays.CustomerSelect;
            }
            else if (_selectedTabIndex == 2)
            {
                _readOnlyArray = ReadOnlyArrays.ReadOnlyColumnArrays.JobSelect;
            }
        }

与TabControl的以下内容绑定:

SelectedIndex="{Binding SelectedTabIndex, Mode=TwoWay}"

我也有:

    public string[] ReadOnlyArray 
    {
        get { return _readOnlyArray; }

        set { _readOnlyArray = value;}
    }

哪个绑定到ComboBox如下:

ItemsSource="{Binding readOnlyArray, Mode=TwoWay}"

我知道我很可能这样做完全错了,但是每当TabControl的标签索引发生变化时,我都希望ComboBox的项目源更新。

1 个答案:

答案 0 :(得分:1)

SelectedTabIndex更改后,您应该通知接口ReadOnlyArray正在更改。假设您的视图模型实现INotifyPropertyChanged,您需要触发适当的事件处理程序:

    set
    {
        _selectedTabIndex = value;
        if (_selectedTabIndex == 0)
        {
            _readOnlyArray = ReadOnlyArrays.ReadOnlyColumnArrays.LoanerItemsSelect;
        }
        else if (_selectedTabIndex == 1)
        {
            _readOnlyArray = ReadOnlyArrays.ReadOnlyColumnArrays.CustomerSelect;
        }
        else if (_selectedTabIndex == 2)
        {
            _readOnlyArray = ReadOnlyArrays.ReadOnlyColumnArrays.JobSelect;
        }

        //Your helper method from base class calling          
        // INotifyPropertyChanged.PropertyChanged event
        this.RaisePropertyChanged("ReadOnlyArray");
    }

如果仍然无效,请查看VisualStudio输出窗口是否存在任何绑定错误。