using MVVMButtonControl.Model; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Controls; using System.Windows.Input; using System.Windows.Media; namespace MVVMButtonControl.ViewModel { internal class ModelViewMain { public ObservableCollection _buttonCollections; public ICommand TestCommand { get; private set; } public ICommand SubButtonCommand { get; private set; } public ModelViewMain() { _buttonCollections = new ObservableCollection(); this.TestCommand = new RelayCommand(CommandMethod); this.SubButtonCommand = new RelayCommand(SubCommandMethod); } public ObservableCollection ButtonCollection { get { return _buttonCollections; } set { if (_buttonCollections != null) _buttonCollections = value; } } private void CommandMethod(object parameter) { for (int i = 0; i < 4; i++) { this.ButtonCollection.Add(new Buttons { Name = $"Button {i}", SelectedColor = Brushes.Transparent }); } } private void SubCommandMethod(object parameter) { for (int i = 0; i < this.ButtonCollection.Count; i++) { this.ButtonCollection[i].SelectedColor = Brushes.Transparent; } Buttons? selected = this.ButtonCollection.Where(b => b.Name.Equals(parameter.ToString())).SingleOrDefault(); if (selected == null) return; selected.SelectedColor = Brushes.LightCoral; } } }