0
votes

I'm rather new to MVVM as a concept and I'm currently trying to set things up so that changing the selected index of a TabControl will change the item source of a ComboBox I have. Currently I have things set up as follows:

    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;
            }
        }

Which is bound to the following of a TabControl:

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

I also have this:

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

        set { _readOnlyArray = value;}
    }

Which is bound to a ComboBox as follows:

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

I know most likely I'm doing this completely wrong but I'd like the ComboBox's item source to update whenever the tab index of the TabControl is changed.

1
Irrelevant question, why is the property called ReadOnlyArray when it defines a public setter? - PoweredByOrange
@programmer93 It's a terrible name. The ItemSource of the ComboBox is a ReadOnly Array. I named it somewhat hastily and plan to change it once things are working. - DanteTheEgregore

1 Answers

1
votes

You should notify the interface that ReadOnlyArray is changing after SelectedTabIndex changes. Assuming your view model implements INotifyPropertyChanged, you need to fire approppriate event hander:

    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");
    }

If it still doesn't work, check out VisualStudio output window for any binding errors.