Ladder AlgoTemplateToolBar - undo/redo cmds, and AddNewItem/Remove behavior

This commit is contained in:
John Colvin committed 2018-12-11 14:18:15 -06:00
1 parent 770d6a9bf5
commit edfa926e95
12 files changed
+169 -84

No files matched your search

@@ -42,7 +42,7 @@
<TreeView
x:Name="_tree_view"
u:TreeViewEx.SelectedItemChangedHandler="{Binding}"
u:EditableCollectionViewEx.NewItemFactory="{Binding definition_factory}"
u:EditableCollectionViewEx.Model="{Binding definition_factory}"
ItemsSource="{Binding definitions}"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
@@ -75,8 +75,7 @@ namespace mbts.spark {
// remove
var remove_spark_cmd = new ObservantSparkCommandCommand<IAlgoStudioContentVM>(Icons.Trash, v => $"Remove '{v?.name}'", v => v != null, value => {
if (value is AlgoDefinitionTemplateVM t) {
t.owner.templates.Remove(t);
_model.algo_model.remove_template(t.id).ignore();
_vm.remove_template(t).ignore();
} else if (value is AlgoDefinitionVM d) {
if (d.templates.Count > 0) {
var resp = BTSMessageBox.Show($"Deleting this definition will also delete it's templates ({d.templates.Count}).\nContinue?", "Algo Studio", BTSMessageBoxButtons.Yes | BTSMessageBoxButtons.No);
@@ -44,13 +44,12 @@ namespace mbts.spark {
: this(model, null) { }
public AlgoDefinitionVM(OrdersModel model, AlgoDefinitionTemplates state)
: this(model, new AlgoDefinitionTemplates?(state)) { }
: this(model, new AlgoDefinitionTemplates?(state), state.record.Key) { }
AlgoDefinitionVM(OrdersModel model, AlgoDefinitionTemplates? state, string id = null) {
_model = model;
algo_roots = new ObservableCollection<IOrderTypeVM>(OrderDataHelper.exchange_order_types.Select(t => OrderTypeVMFactory.create(_model, t, _editing_permissions)));
templates_view = (ListCollectionView)CollectionViewSource.GetDefaultView(templates);
template_factory = () => new AlgoDefinitionTemplateVM(this, model, algo_root.create(), "Untitled");
_selected_template = backing<AlgoDefinitionTemplateVM>(nameof(selected_template));
_can_edit_structure = backing(nameof(can_edit_structure), templates.ObserveSelf(), _ => templates.Count == 0);
if (state.HasValue) {
@@ -132,8 +131,8 @@ namespace mbts.spark {
public ObservableCollection<AlgoDefinitionTemplateVM> templates => _templates;
public IDictionary<AlgoVMKey, AlgoDefinitionTemplateVM> template_map => _templates;
public ListCollectionView templates_view { get; }
public Func<AlgoDefinitionTemplateVM> template_factory { get; }
public AlgoDefinitionTemplateVM template_factory() => new AlgoDefinitionTemplateVM(this, _model, algo_root.create(), "Untitled");
public IObservable<bool> subscribe_is_definition_dirty() => _is_dirty.observe_current(true);
public IObservable<bool> subscribe_any_templates_dirty() => _any_templates_dirty_monitor;
public IObservable<bool> subscribe_is_definition_or_any_templates_dirty() =>
@@ -101,39 +101,6 @@ namespace mbts.spark {
}
}
//class SelectionHelper<T> {
// readonly INotifyCollectionChanged _notify_collection_changed;
// readonly ICollection<T> _collection;
// bool _has_desired_item;
// T _value, _desired_item;
// readonly IEqualityComparer<T> _comparer;
// public SelectionHelper(ICollection<T> collection) {
// if (collection is INotifyCollectionChanged ncc) {
// ncc.CollectionChanged += _on_CollectionChanged;
// }
// }
// public event SelectionChanged<T> selection_changed;
// public T value {
// get => _value;
// set {
// if (!_comparer.Equals(value, _value)) {
// _value = value;
// }
// }
// }
// public void try_select(T item) {
// }
// void _on_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) {
// //switch (e.Action) {
// //}
// }
//}
class OrderPropertiesEditorVM : DisposableViewModelBase, IAlgoTemplateItemToolBarModelStore {
readonly OrdersModel _model;
readonly Property<SecurityImmutable> _security;
@@ -142,6 +109,7 @@ namespace mbts.spark {
readonly SelectorProperty<TimeInForce> _time_in_force;
readonly SelectionModel<AlgoDefinitionVM> _selected_algo_definition;
readonly AlgoDefinitionCollection _algo_definitions;
readonly EditableCollectionModel<AlgoDefinitionTemplateVM, AlgoDefinitionVM> _algo_template_collection_model;
readonly ObservableKeyedCollection<AlgoDefinitionTemplateVM, EditableItemKey<string>> _favorite_templates = new ObservableKeyedCollection<AlgoDefinitionTemplateVM, EditableItemKey<string>>(tvm => tvm.vm_key);
/// Each <see cref="AlgoDefinitionVM"/> has its own algo-definition-templates <see cref="AlgoDefinitionVM.templates"/>.
/// Because WPF ItemsControls do not handle changes to the underlying ItemsSource well (not CollectionChange events, but changing ItemsSource to a new Collection altogether), we must use a single ObservableCollection.
@@ -202,14 +170,19 @@ namespace mbts.spark {
toggle_favorite_cmd = new DelegateCommand<AlgoDefinitionTemplateVM>(tvm => {
_toggle_favorite(tvm.id);
}, tvm => tvm != null);
_algo_template_collection_model = new EditableCollectionModel<AlgoDefinitionTemplateVM, AlgoDefinitionVM>(d => selected_algo_definition == null ? default : selected_algo_definition.template_factory(), d => d != null);
redo_cmd = ObservantCommand.create(
t => t?.redo(),
t => t == null ? Observable.Return(false) : System.Reactive.Linq.ObservableExtensions.FromEvent(cb => t.can_redo_changed += cb, cb => t.can_redo_changed -= cb, () => t.can_redo),
out IObserver<AlgoDefinitionTemplateVM> redo_observer);
undo_cmd = ObservantCommand.create(
t => t?.undo(),
t => t == null ? Observable.Return(false) : System.Reactive.Linq.ObservableExtensions.FromEvent(cb => t.can_undo_changed += cb, cb => t.can_undo_changed -= cb, () => t.can_undo),
out IObserver<AlgoDefinitionTemplateVM> undo_observer);
//
algo_template_factory = () => {
//var ar = new CustomOrderDefinitionImmutable((selected_algo_template == null ? selected_algo_definition.algo_root : selected_algo_template.algo_root).create());
var dtvm = new AlgoDefinitionTemplateVM(selected_algo_definition, _model, new CustomOrderDefinitionImmutable((selected_algo_template == null ? selected_algo_definition.algo_root : selected_algo_template.algo_root).create()), "New Template");
return dtvm;
};
_dispose = new CompositeDisposable(
_selected_algo_definition.subscribe_changed(_selected_algo_definition_Changed),
((IPropertyChangedDemuxer)this).subscribe<AlgoDefinitionTemplateVM>(nameof(selected_algo_template)).Subscribe(undo_observer, redo_observer),
security_source.Subscribe(s => {
using (_selected_algo_definition.begin_update())
_algo_definitions.on_security(s);
@@ -239,6 +212,7 @@ namespace mbts.spark {
_selected_favorite_algo_template.Value = selected_algo_template is AlgoDefinitionTemplateVM t && t.is_persisted && _favorite_template_ids.Contains(t.id) ? t : null;
if (old_value != null)
_algo_templates.remove_collection(old_value.templates);
_algo_template_collection_model.OnNext(new_value);
}
public SecurityImmutable security => _security.Value;
@@ -289,10 +263,12 @@ namespace mbts.spark {
public ICollectionView visible_algo_templates { get; }
public ICommand save_template_cmd { get; }
public ICommand revert_template_changes_cmd { get; }
public ICommand undo_cmd { get; }
public ICommand redo_cmd { get; }
public ICommand remove_template_cmd { get; }
public ICommand add_to_favorites_cmd { get; }
public ICommand toggle_favorite_cmd { get; }
public Func<AlgoDefinitionTemplateVM> algo_template_factory { get; }
public IEditableCollectionModel<AlgoDefinitionTemplateVM> algo_template_collection_model => _algo_template_collection_model;
public bool get_is_default(string template_id) => _default_template_ids.Contains(template_id);
public bool get_is_favorite(string template_id) => _favorite_template_ids.Contains(template_id);
@@ -1,4 +1,5 @@
using System;
using System.Reactive;
using System.Windows.Input;
namespace mbts.ui {
@@ -31,5 +32,35 @@ namespace mbts.ui {
class ObservantCommand : ObservantCommand<object> {
public ObservantCommand(Action execute) : base(_ => execute()) { }
public ObservantCommand(Action<object> execute) : base(execute) { }
public static ICommand create<T>(Action<T> execute, Func<T, bool> get_can_execute, out IObserver<T> observer) {
T param = default;
bool can_execute = false;
var cmd = new DelegateCommand(() => execute(param), () => can_execute);
observer = new AnonymousObserver<T>(p => {
param = p;
if (can_execute != get_can_execute(param))
cmd.RaiseCanExecuteChanged();
});
return cmd;
}
public static ICommand create<T>(Action<T> execute, Func<T, IObservable<bool>> get_can_execute_source, out IObserver<T> observer) {
T param = default;
bool can_execute = false;
var cmd = new DelegateCommand(() => execute(param), () => can_execute);
IDisposable can_execute_sub = null;
observer = new AnonymousObserver<T>(p => {
param = p;
Disposable.dispose(ref can_execute_sub);
can_execute_sub = get_can_execute_source(p).Subscribe(ce => {
if (can_execute != ce) {
can_execute = ce;
cmd.RaiseCanExecuteChanged();
}
});
}, () => Disposable.dispose(ref can_execute_sub));
return cmd;
}
}
}
@@ -331,6 +331,14 @@ namespace System.Reactive.Linq
};
}
public static IObservable<T> FromEvent<T>(Action<Action> add_handler, Action<Action> remove_handler, Func<T> selector) =>
Observable.Create<T>(o => {
callback();
add_handler(callback);
return Disposable.Create(() => remove_handler(callback));
void callback() => o.OnNext(selector());
});
// This is placed here to intentionally make ObserveOnDispatcher ambiguous with the one provided by Rx
public static IObservable<T> ObserveOnDispatcher<T>(this IObservable<T> source) {
throw new InvalidOperationException("This method can starve the UI of input messages. Please use OnDispatcher() instead");
@@ -29,7 +29,7 @@
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding name}" FontWeight="Bold"/>
<c:HelpButton Grid.Column="1" Margin="{StaticResource Margin.LP1}" ToolTip="{Binding description}"/>
<!--<c:HelpButton Grid.Column="1" Margin="2" ToolTip="{Binding description}"/>-->
</Grid>
</DataTemplate>
<usp:AlgoContentTemplateSelector x:Key="AlgoTemplateSelector">
@@ -53,7 +53,7 @@
x:Name="PART_AlgoTemplateToolBar"
Margin="{StaticResource Margin.LP1}"
IsReadOnly="{TemplateBinding IsAlgoTemplateToolBarReadOnly}"
NewItemFactory="{Binding algo_template_factory}"
EditableCollectionModel="{Binding algo_templates}"
Padding="0,1,0,3"
Header="{TemplateBinding Header}"
HeaderTemplate="{StaticResource AlgoTemplateHeaderContentTemplate}"
@@ -91,10 +91,8 @@
</MenuItem.ToolTip>
</MenuItem>
<MenuItem
x:Name="PART_Revert_MenuItem"
Command="{x:Static uag:AlgoTemplateToolBar.BeginRevertCommand}"
CommandTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}"
IsEnabled="False"
x:Name="PART_Undo_MenuItem"
Command="{TemplateBinding UndoCommand}"
HorizontalContentAlignment="Center"
Style="{StaticResource AlgoMenuItem}"
>
@@ -105,6 +103,19 @@
<TextBlock Text="{Binding SelectedItem.name, RelativeSource={RelativeSource TemplatedParent}, StringFormat='{}Undo changes to \&apos;{0}\&apos;'}"/>
</MenuItem.ToolTip>
</MenuItem>
<MenuItem
x:Name="PART_Redo_MenuItem"
Command="{TemplateBinding RedoCommand}"
HorizontalContentAlignment="Center"
Style="{StaticResource AlgoMenuItem}"
>
<MenuItem.Header>
<Image Source="{x:Static u:Icons.RedoCircular}"/>
</MenuItem.Header>
<MenuItem.ToolTip>
<TextBlock Text="{Binding SelectedItem.name, RelativeSource={RelativeSource TemplatedParent}, StringFormat='{}Redo changes to \&apos;{0}\&apos;'}"/>
</MenuItem.ToolTip>
</MenuItem>
<MenuItem
x:Name="PART_IsFavorite_MenuItem"
u:Behavior.AutoRaiseCanExecuteChanged="True"
@@ -126,12 +137,12 @@
</Trigger>
<Trigger Property="SelectedItem" Value="{x:Null}">
<Setter TargetName="PART_Rename_MenuItem" Property="IsEnabled" Value="False"/>
<Setter TargetName="PART_Revert_MenuItem" Property="ToolTip" Value="{x:Null}"/>
<Setter TargetName="PART_Undo_MenuItem" Property="ToolTip" Value="{x:Null}"/>
<Setter TargetName="PART_Redo_MenuItem" Property="ToolTip" Value="{x:Null}"/>
</Trigger>
<Trigger Property="IsEdited" Value="True">
<Setter TargetName="PART_Templates_ComboBox" Property="BorderBrush" Value="{u:BTSColor UI.input_dirty_border}"/>
<Setter TargetName="PART_Save_MenuItem" Property="IsEnabled" Value="True"/>
<Setter TargetName="PART_Revert_MenuItem" Property="IsEnabled" Value="True"/>
</Trigger>
<Trigger Property="IsEditing" Value="True">
<Setter TargetName="PART_Templates_ComboBox" Property="SelectedItem" Value="{Binding EditingItemPlaceholder, RelativeSource={RelativeSource Self}, Mode=TwoWay}"/>
@@ -27,6 +27,8 @@ namespace mbts.ui.spark.algo {
BeginSaveCommand = new RoutedUICommand("Save Template", "Save", typeof(AlgoTemplateToolBar)),
BeginSaveAsCommand = new RoutedUICommand("Save Template As", "Save As", typeof(AlgoTemplateToolBar)),
BeginRevertCommand = new RoutedUICommand("Revert to Template", "Revert", typeof(AlgoTemplateToolBar)),
BeginRedoCommand = new RoutedUICommand("Redo", "Redo", typeof(AlgoTemplateToolBar)),
BeginUndoCommand = new RoutedUICommand("Undo", "Undo", typeof(AlgoTemplateToolBar)),
BeginRemoveCommand = new RoutedUICommand("Delete Template", "Delete", typeof(AlgoTemplateToolBar));
public static readonly ExecutedRoutedEventHandler
@@ -34,6 +36,8 @@ namespace mbts.ui.spark.algo {
BeginSaveExecuted = WPFHelper.create_cmd_executed<object, AlgoTemplateToolBar>((tb, _) => tb._save()),
BeginSaveAsExecuted = WPFHelper.create_cmd_executed<object, AlgoTemplateToolBar>((tb, _) => tb._save_as()),
BeginRevertExecuted = WPFHelper.create_cmd_executed<object, AlgoTemplateToolBar>((tb, _) => tb._revert()),
BeginRedoExecuted = WPFHelper.create_cmd_executed<object, AlgoTemplateToolBar>((tb, _) => tb._redo()),
BeginUndoExecuted = WPFHelper.create_cmd_executed<object, AlgoTemplateToolBar>((tb, _) => tb._undo()),
BeginRemoveExecuted = WPFHelper.create_cmd_executed<object, AlgoTemplateToolBar>((tb, t) => tb._remove(t));
ComboBox _templates_combo;
@@ -72,10 +76,10 @@ namespace mbts.ui.spark.algo {
public AlgoDefinitionTemplateVM SelectedItem { get => GetValue(SelectedItemProperty) as AlgoDefinitionTemplateVM; set => SetValue(SelectedItemProperty, value); }
#endregion
#region NewItemFactory
public static readonly DependencyProperty NewItemFactoryProperty =
WPFHelper.create_dp<Func<AlgoDefinitionTemplateVM>, AlgoTemplateToolBar>(nameof(NewItemFactory));
public Func<AlgoDefinitionTemplateVM> NewItemFactory { get => GetValue(NewItemFactoryProperty) as Func<AlgoDefinitionTemplateVM>; set => SetValue(NewItemFactoryProperty, value); }
#region EditableCollectionModel
public static readonly DependencyProperty EditableCollectionModelProperty =
WPFHelper.create_dp<IEditableCollectionModel<AlgoDefinitionTemplateVM>, AlgoTemplateToolBar>(nameof(EditableCollectionModel));
public IEditableCollectionModel<AlgoDefinitionTemplateVM> EditableCollectionModel { get => GetValue(EditableCollectionModelProperty) as IEditableCollectionModel<AlgoDefinitionTemplateVM>; set => SetValue(EditableCollectionModelProperty, value); }
#endregion
#region AddNew
@@ -102,6 +106,18 @@ namespace mbts.ui.spark.algo {
public ICommand RemoveCommand { get => GetValue(RemoveCommandProperty) as ICommand; set => SetValue(RemoveCommandProperty, value); }
#endregion
#region Undo
public static readonly DependencyProperty UndoCommandProperty =
WPFHelper.create_dp<ICommand, AlgoTemplateToolBar>(nameof(UndoCommand));
public ICommand UndoCommand { get => GetValue(UndoCommandProperty) as ICommand; set => SetValue(UndoCommandProperty, value); }
#endregion
#region Redo
public static readonly DependencyProperty RedoCommandProperty =
WPFHelper.create_dp<ICommand, AlgoTemplateToolBar>(nameof(RedoCommand));
public ICommand RedoCommand { get => GetValue(RedoCommandProperty) as ICommand; set => SetValue(RedoCommandProperty, value); }
#endregion
#region ToggleFavorite
public static readonly DependencyProperty ToggleFavoriteCommandProperty =
WPFHelper.create_dp<ICommand, AlgoTemplateToolBar>(nameof(ToggleFavoriteCommand));
@@ -139,14 +155,7 @@ namespace mbts.ui.spark.algo {
static readonly DependencyPropertyKey IsEditedPropertyKey =
WPFHelper.create_ro_dp<bool, AlgoTemplateToolBar>(
nameof(IsEdited),
(o, v) => {
if (v == o._editable_items.IsEditingItem) {
// don't expect this will happen, but check it anyways to guard against future code changes that might break this contract.
return;
}
if (v) o._editable_items.EditItem(o.SelectedItem);
else o._editable_items.CancelEdit();
},
null,
(o, v) => o.SelectedItem?.is_dirty ?? false);
internal static readonly DependencyProperty IsEditedProperty = IsEditedPropertyKey.DependencyProperty;
internal bool IsEdited { get => (bool)GetValue(IsEditedProperty); private set => SetValue(IsEditedPropertyKey, value); }
@@ -173,6 +182,7 @@ namespace mbts.ui.spark.algo {
switch (e.Key) {
case Key.Enter:
Keyboard.ClearFocus();
_commit();
break;
case Key.Escape:
_cancel();
@@ -192,7 +202,7 @@ namespace mbts.ui.spark.algo {
// 2) The user is renaming an existing template.
// In both cases, committing the text is reasonable (and probably expected) behavior.
// Note: This doesn't mean it is impossible to cancel such changes - the user may do so by pressing Esc.
_commit();
//_commit();
}
}
@@ -204,37 +214,48 @@ namespace mbts.ui.spark.algo {
#region Transactional
void _add_new() {
_prepare_edit();
var factory = NewItemFactory;
if (factory != null) {
_editable_items.AddNewItem(factory());
} else _editable_items.AddNew();
var model = EditableCollectionModel;
if (model != null && model.can_add_new)
_editable_items.AddNewItem(model.new_item_factory());
}
void _cancel() => _editable_items.CancelAny();
void _commit() {
void _commit(bool execute_save = false) {
if (_editable_items.IsEditingItem) {
var editing_item = (AlgoDefinitionTemplateVM)_editable_items.CurrentEditItem;
editing_item.name = _templates_combo.Text;
_editable_items.CommitEdit();
SetCurrentValue(SelectedItemProperty, editing_item);
SaveCommand.TryExecuteIfCan(editing_item);
if (execute_save)
SaveCommand.TryExecuteIfCan(editing_item);
SetCurrentValue(IsEditingProperty, false);
} else if (_editable_items.IsAddingNew) {
var new_item = (AlgoDefinitionTemplateVM)_editable_items.CurrentAddItem;
new_item.name = _templates_combo.Text;
_editable_items.CommitNew();
SetCurrentValue(SelectedItemProperty, new_item);
SaveCommand.TryExecuteIfCan(new_item);
if (execute_save)
SaveCommand.TryExecuteIfCan(new_item);
SetCurrentValue(IsEditingProperty, false);
}
}
void _revert() {
_cancel();
//_cancel();
RevertCommand.TryExecuteIfCan(null);
}
void _redo() {
if (SelectedItem is AlgoDefinitionTemplateVM t && t.can_redo)
t.redo();
}
void _undo() {
if (SelectedItem is AlgoDefinitionTemplateVM t && t.can_undo)
t.undo();
}
void _remove(object template) {
_editable_items?.Remove(template);
RemoveCommand.TryExecuteIfCan(template);
@@ -243,7 +264,7 @@ namespace mbts.ui.spark.algo {
void _save() {
if (SelectedItem == null) {
_add_new();
} else _commit();
} else _commit(true);
}
void _save_as() {
@@ -94,13 +94,14 @@
x:Name="PART_AlgoTemplateToolBar"
Margin="{StaticResource Margin.LP1}"
IsReadOnly="{TemplateBinding IsAlgoTemplateToolBarReadOnly}"
NewItemFactory="{Binding algo_template_factory}"
EditableCollectionModel="{Binding algo_template_collection_model}"
Padding="0,1,0,3"
Header="{Binding SelectedItem.algo_root.order_type, RelativeSource={RelativeSource AncestorType=uag:OrderDetailsPopupControl}, Converter={StaticResource OrderTypeDisplayTextConverter}}"
HeaderTemplate="{StaticResource AlgoTemplateHeaderContentTemplate}"
ItemModelStore="{Binding}"
RemoveCommand="{Binding remove_template_cmd}"
RevertCommand="{Binding revert_template_changes_cmd}"
UndoCommand="{Binding undo_cmd}"
RedoCommand="{Binding redo_cmd}"
SaveCommand="{Binding save_template_cmd}"
ToggleFavoriteCommand="{Binding toggle_favorite_cmd}"
SelectedItem="{Binding selected_algo_template, Mode=TwoWay}"
@@ -1,6 +1,7 @@
using bts.utils;
using System;
using System.ComponentModel;
using System.Reactive;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
@@ -36,6 +37,43 @@ namespace mbts.ui {
public static bool operator !=(EditableItemKey<T> a, EditableItemKey<T> b) => !a.Equals(b);
}
interface IEditableCollectionModel {
event Action can_add_new_changed;
object new_item_factory();
bool can_add_new { get; }
}
interface IEditableCollectionModel<T> : IEditableCollectionModel {
new T new_item_factory();
}
class EditableCollectionModel<T, TParameter> : IEditableCollectionModel<T>, IObserver<TParameter> {
TParameter _parameter;
readonly Func<TParameter, bool> _get_can_add_new;
readonly Func<TParameter, T> _new_item_factory;
public EditableCollectionModel(Func<TParameter, T> new_item_factory, Func<TParameter, bool> can_add_new, TParameter init = default) {
_new_item_factory = new_item_factory;
_get_can_add_new = can_add_new;
_parameter = init;
this.can_add_new = _get_can_add_new(_parameter);
}
public bool can_add_new { get; private set; }
public event Action can_add_new_changed;
public T new_item_factory() => _new_item_factory(_parameter);
public void OnCompleted() { }
public void OnError(Exception error) { }
public void OnNext(TParameter value) {
_parameter = value;
if (can_add_new != _get_can_add_new(value)) {
can_add_new = !can_add_new;
can_add_new_changed?.Invoke();
}
}
object IEditableCollectionModel.new_item_factory() => new_item_factory();
}
static class EditableCollectionViewEx {
public static readonly RoutedUICommand
AddNewItemCommand = new RoutedUICommand("Add New Item", "Add New", typeof(EditableCollectionViewEx)),
@@ -47,8 +85,9 @@ namespace mbts.ui {
AddNewItemExecuted = (s, e) => {
var collection = _get_collection(s, e);
if (collection != null) {
if (collection is IEditableCollectionViewAddNewItem cani && cani.CanAddNewItem && GetNewItemFactory(s as DependencyObject) is Func<object> factory) {
cani.AddNewItem(factory());
if (GetModel((DependencyObject)s) is IEditableCollectionModel model) {
if (model.can_add_new && collection is IEditableCollectionViewAddNewItem cani && cani.CanAddNewItem)
cani.AddNewItem(model.new_item_factory());
} else if (collection.CanAddNew) {
collection.AddNew();
}
@@ -74,14 +113,14 @@ namespace mbts.ui {
public static readonly CanExecuteRoutedEventHandler
AddNewItemCanExecute = (s, e) => e.CanExecute = true;
public static readonly DependencyProperty NewItemFactoryProperty =
public static readonly DependencyProperty ModelProperty =
DependencyProperty.RegisterAttached(
"NewItemFactory",
typeof(Func<object>),
"Model",
typeof(IEditableCollectionModel),
typeof(EditableCollectionViewEx));
public static Func<object> GetNewItemFactory(DependencyObject d) => d.GetValue(NewItemFactoryProperty) as Func<object>;
public static void SetNewItemFactory(DependencyObject d, Func<object> value) => d.SetValue(NewItemFactoryProperty, value);
public static IEditableCollectionModel GetModel(DependencyObject d) => d.GetValue(ModelProperty) as IEditableCollectionModel;
public static void SetModel(DependencyObject d, IEditableCollectionModel value) => d.SetValue(ModelProperty, value);
static IEditableCollectionView _assert_collection(ItemsControl items_control) =>
items_control.ItemsSource as IEditableCollectionView ??