0

Есть класс:

using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Media;

namespace Games
{
    public class StudentModel : INotifyPropertyChanged
    {
        public string Character { get; set; }
        public string Question { get; set; }
        public int ReceiverId { get; set; }
        private int remoteId;
        public int RemoteId
        {
            get => remoteId;
            set
            {
                if (remoteId == value) return;
                remoteId = value;
                RaisePropertyChanged(nameof(RemoteId));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public StudentModel(int remoteId, int receiverId)
        {
            RemoteId = remoteId;
            ReceiverId = receiverId;
        }
        public StudentModel()
        {

        }

        protected virtual void RaisePropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

Так же есть коллекция:

using System;
using System.Collections.ObjectModel;
using System.Linq;

namespace Games
{
    public class MainWindowViewModel
    {
        public ObservableCollection<StudentModel> Students { get; set; } = new ObservableCollection<StudentModel>();

        readonly DeviceManager deviceManager = new DeviceManager(new VotumDevicesManager());

        public StudentModel SelectedStudent { get; set; }

        public MainWindowViewModel()
        {
            for (int i = 0; i < 32; i++)
            {
                Students.Add(new StudentModel());
            }

            deviceManager.votumManager.ButtonClicked += VotumManager_ButtonClicked;
        }
        private void VotumManager_ButtonClicked(object sender, ButtonClickEventArgs e)
        {
            EnsureRemoteAdded(e.RemoteId, e.ReceiverId);
        }

        private void EnsureRemoteAdded(int RemoteId, int ReceiverId)
        {
            if (!Students.Any(x => x.ReceiverId.Equals(ReceiverId) && x.RemoteId.Equals(RemoteId)))
            {
                App.Current.Dispatcher.BeginInvoke(new Action(() =>
                {
                    //Students.Update(x => x.RemoteId = RemoteId);
                    //var std = Students.FirstOrDefault(x => x.RemoteId == 0 && x !=null);
                    //Students.FirstOrDefault().RemoteId = RemoteId;
                    /*var std = */Students.FirstOrDefault(x => x.RemoteId.Equals(RemoteId));
                    //if (std != null)
                    //    std.RemoteId = RemoteId;
                }));
            }
        }
    }
}

Во втором классе я заполняю коллекцию пустыми объектами, чтобы отобразить на DataGrid таблицу (пустая таблица состоящая из 32 строк - требование такое). Как дело обстоит - в обработчике события нажатия кнопки я получаю значение, которое мне нужно записать в переменную любого объекта в коллекции и хранить его там и так для каждого объекта, так же надо отсортировать её поэтому значению. Собственно, в этом и вопрос, как мне сделать это? В методе EnsureRemoteAdded есть несколько вариантов и они все рабочие, но результат они дают не тот, который нужен.

0

1 ответ 1

0

Вопрос решён самостоятельно:

var std = Students.FirstOrDefault(x => x.RemoteId.Equals(RemoteId) || x.RemoteId == 0); 
if (std != null) 
     std.RemoteId = RemoteId;

Ваш ответ

Нажимая на кнопку «Отправить ответ», вы соглашаетесь с нашими условиями использования и подтверждаете, что прочитали и поняли наши политику конфиденциальности и нормы поведения.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.