выбрать сетку данных из нескольких строк в зависимости от свойства, существующего в Viewmodel WPF MVVM - PullRequest
0 голосов
/ 26 февраля 2019

Я пытаюсь выбрать несколько строк таблицы данных из свойства viewmodel.Я создал свойство observablecollection, представляющее собой коллекцию сотрудников, которую я хотел бы связать с выбранными элементами данных.для этого я создал свойство зависимости.

Я устанавливаю свойство observablecollection из viewmodel, но оно не обновляется в представлении (XAML).

Код модели представления:

    public MainWindowViewModel()
    {
        EmployeeCollection = new ObservableCollection<Employee>()
        {
            new Employee(){ Id= 1, Department = "Comp", Name= "X" },
            new Employee(){ Id= 2, Department = "Comp1", Name= "X1" },
            new Employee(){ Id= 3, Department = "Comp2", Name= "X2" },
        };
    }

    private ObservableCollection<Employee> _employeeCollection;

    public ObservableCollection<Employee> EmployeeCollection
    {
        get { return _employeeCollection; }
        set { _employeeCollection = value; }
    }

    private IList<Employee> _selectedMutipleEmployeeCollection;

    public IList<Employee> SelectedMutipleEmployeeCollection
    {
        get { return _selectedMutipleEmployeeCollection; }
        set
        {
            _selectedMutipleEmployeeCollection = value;
            OnPropertyChanged("SelectedMutipleEmployeeCollection");
        }
    }


    private Employee _employee;

    public Employee SelectedEmployee
    {
        get { return _employee; }
        set
        {
            _employee = value;
            OnPropertyChanged("SelectedEmployee");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;

        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    private ICommand _singleCommand;

    public ICommand SingleCommand
    {
        get
        {
            if (_singleCommand == null)
            {
                _singleCommand = new DelegateCommand((obj) => SelectSingleRow());
            }
            return _singleCommand;
        }
    }

    private void SelectSingleRow()
    {
        SelectedEmployee = EmployeeCollection.FirstOrDefault(x => x.Id == 3);
    }


    private ICommand _multiCommand;

    public ICommand MultipleCommand
    {
        get
        {
            if (_multiCommand == null)
            {
                _multiCommand = new DelegateCommand((obj) => SelectMultipleRow());
            }
            return _multiCommand;
        }
    }

    private void SelectMultipleRow()
    {
        SelectedMutipleEmployeeCollection = EmployeeCollection.Where(x => x.Id <=3).ToList();
    }

Код XAML:

<StackPanel>
        <local:DataGridBehavior ItemsSource="{Binding EmployeeCollection}" SelectedItem="{Binding SelectedEmployee}" SelectedItems="{Binding SelectedMutipleEmployeeCollection}">
        </local:DataGridBehavior>
        <StackPanel Orientation="Horizontal">
            <Button Height="50" Width="100" Margin="15" Content="Select Single Row" Command="{Binding SingleCommand}"/>
            <Button Height="50" Width="100" Content="Select Multiple Row" Command="{Binding MultipleCommand}"/>
        </StackPanel>
    </StackPanel>

DataGridBehavior:

    public static readonly DependencyProperty SelectedItemsProperty =
    DependencyProperty.Register("SelectedItems", typeof(IList<object>), typeof(DataGridBehavior));


    public IList<object> SelectedItems
    {
        get
        {
            return (IList<object>)GetValue(SelectedItemsProperty);
        }
        set
        {
            SetValue(SelectedItemsProperty, value);
        }
    }

    private static void SelectedItemsPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {

    }

    protected override void OnSelectionChanged(SelectionChangedEventArgs e)
    {
        base.OnSelectionChanged(e);
        SetValue(SelectedItemsProperty, base.SelectedItems);
        this.Focus();

    }
}
...