dimanche 10 mai 2015

How to access View control's from the ViewModel?

I've come across the situation, where I need access to the controls in the View from the VM. In order to code a method that adds the selected item from a ComboBox to a list.

My question is how do I access the View controls from the ViewModel? Is there a certain design pattern I should follow to allow this?

The method below is coded in the View's code behind, which I know is bad practice if following the MVVM pattern, due to the tight coupling involved. Which is why I'm aiming to move this method the the VM.

The method's purpose is to take the currently selected items from two ComboBoxes and add to a Key/Value List:

            public void AddGradeSubjectChoiceToList()
            {

                string SelectedSubjectName = "null data";
                int SelectedPoints = 01;


                SelectedSubjectName = subjectCmbBx.SelectedItem.ToString();

                try {

                SelectedPoints = int.Parse(ordinaryGradeCmbBx.SelectedValue.ToString());

                }
                catch (Exception e)
                {
                    //log error here..

                }

                List<StringKeyValue> SubjectPointKVTemp = new List<StringKeyValue>();

                //Add selected pair to list
                SubjectPointKVTemp.Add(new StringKeyValue { Key = SelectedSubjectName, Value = SelectedPoints });

                SubjectPointKV = SubjectPointKVTemp;



           }

The stripped down implementation of the ViewModel is as follows for reference, showing in comments how I pan to implement the AddGradeSubjectChoiceToList method:

namespace LC_Points.ViewModel
{

    public class MainViewModel : ViewModelBase
    {


        private ScoreModel _scoreModel;


        /// <summary>
        /// Initializes a new instance of the MainViewModel class.
        /// </summary>
        public MainViewModel(ScoreModel GradeModel)
        {

            _scoreModel = GradeModel;

            //call methods to initilise list data
            GetSubjectTypes();
            GetOrdinaryGradePairs();
        }


        public List<ScoreModel> Subjects { get; set; }
        public List<StringKeyValue> OrdinaryGradePointKV { get; set; }


        //ordinary toggle button bool
        private bool _isOrdinaryToggled;
        public bool IsOrdinaryToggled
        {
            get
            {
                return _isOrdinaryToggled;
            }
            set
            {
                _isOrdinaryToggled = value;
                RaisePropertyChanged("IsOrdinaryToggled");
            }
        }



        //Need to add same method from code behind to VM here
        //but don't have access to the View's controlsMethod to store Subject and Grade from Combo Boxes
        public void AddGradeSubjectChoiceToList()
        {

        }


        //This Relay Command is tied to the button in the View, that will be used
        //to call the AddGradeSubjectChoiceToList method
        RelayCommand addGradeCommand;
        public RelayCommand AddGradeCommand
        {
            get
            {
                if (addGradeCommand == null)
                {
                    addGradeCommand = new RelayCommand(() =>
                    {
                       AddGradeSubjectChoiceToList
                    });
                }
                return addGradeCommand;
            }
        }



        public class StringKeyValue
        {
            public string Key { get; set; }
            public int Value { get; set; }
        }


        public void GetOrdinaryGradePairs()
        {

            List<StringKeyValue> ordinaryGradePointKVTemp = new List<StringKeyValue>();


            ordinaryGradePointKVTemp.Add(new StringKeyValue { Key = "A1", Value = 60 });
            ordinaryGradePointKVTemp.Add(new StringKeyValue { Key = "A2", Value = 50 });
            ordinaryGradePointKVTemp.Add(new StringKeyValue { Key = "B1", Value = 45 });
            ordinaryGradePointKVTemp.Add(new StringKeyValue { Key = "B2", Value = 40 });
            ordinaryGradePointKVTemp.Add(new StringKeyValue { Key = "B3", Value = 35 });
            ordinaryGradePointKVTemp.Add(new StringKeyValue { Key = "C1", Value = 30 });
            ordinaryGradePointKVTemp.Add(new StringKeyValue { Key = "C2", Value = 25 });
            ordinaryGradePointKVTemp.Add(new StringKeyValue { Key = "C3", Value = 20 });


            OrdinaryGradePointKV = ordinaryGradePointKVTemp;

        }





        public void GetSubjectTypes()
        {
            List<ScoreModel> subjectList = new List<ScoreModel>();

            // Adding Subjects to List
            subjectList.Add(new ScoreModel { Subject = "Accounting" });
            subjectList.Add(new ScoreModel { Subject = "Agricultural Economics" });
            subjectList.Add(new ScoreModel { Subject = "Agricultural Science" });
            subjectList.Add(new ScoreModel { Subject = "Ancient Greek" });
            subjectList.Add(new ScoreModel { Subject = "Applied Math" });
            subjectList.Add(new ScoreModel { Subject = "Arabic" });
            subjectList.Add(new ScoreModel { Subject = "Art" });
            subjectList.Add(new ScoreModel { Subject = "Artistic & Creative Group" });
            subjectList.Add(new ScoreModel { Subject = "Biology" });
            subjectList.Add(new ScoreModel { Subject = "Business" });


            Subjects = subjectList;

        }

    }
}

Aucun commentaire:

Enregistrer un commentaire