57 lines
2.9 KiB
C#
57 lines
2.9 KiB
C#
using mbts.collections;
|
|
using mbts.ui.controls;
|
|
using mbts.ui.shell;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Collections.Specialized;
|
|
using System.Linq;
|
|
using System.Reactive.Linq;
|
|
|
|
namespace mbts.spark.utilities
|
|
{
|
|
internal class GridRowViewModelBase<TColumn> : DisposableViewModelBase
|
|
{
|
|
private readonly IList<ColumnContainer> _column_containers = new List<ColumnContainer>();
|
|
private readonly ObservableCollectionEx<TColumn> _selected_columns = new ObservableCollectionEx<TColumn>();
|
|
|
|
private void _check_column_present(TColumn column) { if (_column_containers.Any(c => c.column.Equals(column))) throw new InvalidOperationException("Column '{0}' already added.".fmt(column)); }
|
|
|
|
public SelectableViewModelBase<T> AddCell<T>(TColumn column) { return AddCell(column, default(T)); }
|
|
public SelectableViewModelBase<T> AddCell<T>(TColumn column, T initial_value) { return AddCell(column, initial_value, Comparer<T>.Default); }
|
|
public SelectableViewModelBase<T> AddCell<T>(TColumn column, T initial_value, IComparer<T> comparer) {
|
|
_check_column_present(column);
|
|
var cell = new SelectableViewModelBase<T>(initial_value, comparer);
|
|
_column_containers.Add(new ColumnContainer(column, cell.observe_is_selected(), this));
|
|
return cell;
|
|
}
|
|
|
|
public RXReadOnlyDataGridCell<T> AddCell<T>(TColumn column, IObservable<T> data) { return AddCell(column, data, default(T)); }
|
|
public RXReadOnlyDataGridCell<T> AddCell<T>(TColumn column, IObservable<T> data, T initial_value) {
|
|
_check_column_present(column);
|
|
var cell = new RXReadOnlyDataGridCell<T>(data, initial_value);
|
|
_column_containers.Add(new ColumnContainer(column, cell.observe_is_selected(), this));
|
|
return cell;
|
|
}
|
|
|
|
public void RemoveCell(TColumn column) { _column_containers.PopDispose(column, c => c.column); }
|
|
|
|
public bool is_selected => _selected_columns.Any();
|
|
public IReadOnlyObservableCollectionEx<TColumn> selected_columns => _selected_columns;
|
|
public IObservable<bool> observe_is_selected() { return _selected_columns.Observe().Select(_ => _selected_columns.Count > 0).DistinctUntilChanged(); }
|
|
|
|
protected override void Dispose(bool disposing) { Disposable.dispose(_column_containers); }
|
|
|
|
private class ColumnContainer : IDisposable {
|
|
public ColumnContainer(TColumn column, IObservable<bool> is_selected, GridRowViewModelBase<TColumn> owner) {
|
|
this.column = column;
|
|
_dispose = is_selected.OnDispatcher().Subscribe(selected => {
|
|
if (selected) owner._selected_columns.Add(column);
|
|
else owner._selected_columns.Remove(column);
|
|
});
|
|
}
|
|
public readonly TColumn column;
|
|
private IDisposable _dispose;
|
|
public void Dispose() { Disposable.dispose(ref _dispose); }
|
|
}
|
|
}
|
|
} |