diff --git a/src/client/spark/view/Controls/ColorPicker/ColorPickerControl.xaml b/src/client/spark/view/Controls/ColorPicker/ColorPickerControl.xaml index dc6232211..0a3752e24 100644 --- a/src/client/spark/view/Controls/ColorPicker/ColorPickerControl.xaml +++ b/src/client/spark/view/Controls/ColorPicker/ColorPickerControl.xaml @@ -74,7 +74,7 @@ - + diff --git a/src/client/spark/view/Controls/ColorPicker/ColorPickerControl.xaml.cs b/src/client/spark/view/Controls/ColorPicker/ColorPickerControl.xaml.cs index db611cbcc..a0aff2638 100644 --- a/src/client/spark/view/Controls/ColorPicker/ColorPickerControl.xaml.cs +++ b/src/client/spark/view/Controls/ColorPicker/ColorPickerControl.xaml.cs @@ -24,7 +24,6 @@ namespace mbts.ui.controls.color_picker { partial class ColorPickerControl : Control { const string _max_saturation_color_key = "MaxSaturationColor"; readonly PropertyUpdateGate _saturation_border_gate = new PropertyUpdateGate(); - readonly ColorPickerVM _color_vm = new ColorPickerVM(); DataGrid _named_colors_grid; Border _saturation_border; @@ -34,7 +33,6 @@ namespace mbts.ui.controls.color_picker { public ColorPickerControl() { InitializeComponent(); - DataContext = _color_vm; CommandManager.AddExecutedHandler(this, executed_handler); CommandManager.AddCanExecuteHandler(this, can_execute_handler); @@ -50,10 +48,10 @@ namespace mbts.ui.controls.color_picker { _named_colors_grid.CommitEdit(); } else if (e.Parameter is NamedColorVM vm) { if (e.Command == ColorPickerSwatch.AssignColorCommand) - vm.assign_color(_color_vm.result); + vm.assign_color(Model.result); else if (e.Command == ColorPickerSwatch.NameColorCommand) { _named_colors_tab.IsSelected = true; - _color_vm.selected_named_color = vm; + Model.selected_named_color = vm; _named_colors_grid.ScrollIntoView(vm); _named_colors_grid.CurrentCell = new DataGridCellInfo(vm, _named_colors_grid.Columns[0]); } @@ -64,9 +62,11 @@ namespace mbts.ui.controls.color_picker { e.CanExecute = true; } + public ColorPickerVM Model { get; } = new ColorPickerVM(); + #region SelectedColor public static readonly DependencyProperty SelectedColorProperty = - WPFHelper.create_dp(nameof(SelectedColor), (cp, val) => cp._color_vm.result = val); + WPFHelper.create_dp( nameof(SelectedColor), (cp, val) => cp.Model.result = val); public Color SelectedColor { get => (Color)GetValue(SelectedColorProperty); set => SetValue(SelectedColorProperty, value); } @@ -95,17 +95,17 @@ namespace mbts.ui.controls.color_picker { // b) destroy and recreate color_vm without breaking TabControl bindings, and ICollectionView source dependencies void _on_palettes_collection_changed(object sender, NotifyCollectionChangedEventArgs e) { switch (e.Action) { - case NotifyCollectionChangedAction.Add: _color_vm.palettes.InsertRange(e.NewStartingIndex, e.NewItems); break; - case NotifyCollectionChangedAction.Remove: _color_vm.palettes.RemoveRange(e.OldStartingIndex, e.OldItems.Count); break; - case NotifyCollectionChangedAction.Move: _color_vm.palettes.Move(e.OldStartingIndex, e.NewStartingIndex); break; + case NotifyCollectionChangedAction.Add: Model.palettes.InsertRange(e.NewStartingIndex, e.NewItems); break; + case NotifyCollectionChangedAction.Remove: Model.palettes.RemoveRange(e.OldStartingIndex, e.OldItems.Count); break; + case NotifyCollectionChangedAction.Move: Model.palettes.Move(e.OldStartingIndex, e.NewStartingIndex); break; case NotifyCollectionChangedAction.Replace: - _color_vm.palettes.RemoveRange(e.OldStartingIndex, e.OldItems.Count); - _color_vm.palettes.InsertRange(e.NewStartingIndex, e.NewItems); + Model.palettes.RemoveRange(e.OldStartingIndex, e.OldItems.Count); + Model.palettes.InsertRange(e.NewStartingIndex, e.NewItems); break; case NotifyCollectionChangedAction.Reset: - _color_vm.palettes.Clear(); + Model.palettes.Clear(); if (sender is IEnumerable items) - _color_vm.palettes.AddRange(items); + Model.palettes.AddRange(items); break; } } @@ -119,17 +119,17 @@ namespace mbts.ui.controls.color_picker { _saturation_border = this.assert_template_child("_saturation_border"); _sample_image = this.assert_template_child("_sample_image"); // TabItems do not inherit DataContext properly from its visual parent, which breaks certain bindings - _named_colors_grid.SetBinding(DataGrid.ItemsSourceProperty, new Binding(nameof(ColorPickerVM.all_colors_view)) { Source = _color_vm }); - _named_colors_grid.SetBinding(DataGrid.SelectedItemProperty, new Binding(nameof(ColorPickerVM.selected_named_color)) { Source = _color_vm }); - _color_vm.PropertyChanged += color_vm_property_changed; - _color_vm.result = SelectedColor; + _named_colors_grid.SetBinding(DataGrid.ItemsSourceProperty, new Binding(nameof(ColorPickerVM.all_colors_view)) { Source = Model }); + _named_colors_grid.SetBinding(DataGrid.SelectedItemProperty, new Binding(nameof(ColorPickerVM.selected_named_color)) { Source = Model }); + Model.PropertyChanged += color_vm_property_changed; + Model.result = SelectedColor; IDisposable mouse_move_hook = null; _saturation_border.MouseDown += saturation_border_mouse_down; _saturation_border.MouseUp += saturation_border_mouse_up; _template_hooks = Disposable.Create(() => { _named_colors_grid.clear_bindings(DataGrid.ItemsSourceProperty, DataGrid.SelectedItemProperty); - _color_vm.PropertyChanged -= color_vm_property_changed; + Model.PropertyChanged -= color_vm_property_changed; _saturation_border.MouseDown -= saturation_border_mouse_down; _saturation_border.MouseUp -= saturation_border_mouse_up; Disposable.dispose(ref mouse_move_hook); @@ -138,7 +138,7 @@ namespace mbts.ui.controls.color_picker { void color_vm_property_changed(object sender, PropertyChangedEventArgs e) { switch (e.PropertyName) { case nameof(ColorPickerVM.max_saturation): _update_saturation_img(); break; - case nameof(ColorPickerVM.result): SelectedColor = _color_vm.result; break; + case nameof(ColorPickerVM.result): SelectedColor = Model.result; break; } } @@ -162,7 +162,7 @@ namespace mbts.ui.controls.color_picker { var left_adj = ColorUtils.adjust_color(tr, Colors.White, left); var result = ColorUtils.adjust_color(left_adj, Colors.Black, down); using (_saturation_border_gate.update()) - _color_vm.result = result; + Model.result = result; } } @@ -173,7 +173,7 @@ namespace mbts.ui.controls.color_picker { void _update_saturation_img() => _saturation_border_gate.try_update(() => - Resources[_max_saturation_color_key] = _color_vm.max_saturation); + Resources[_max_saturation_color_key] = Model.max_saturation); void _sample_image_click(BitmapSource img, Point pos) { // https://social.msdn.microsoft.com/Forums/vstudio/en-US/82a5731e-e201-4aaf-8d4b-062b138338fe/getting-pixel-information-from-a-bitmapimage?forum=wpf @@ -188,9 +188,9 @@ namespace mbts.ui.controls.color_picker { byte red = pixels[index]; byte green = pixels[index + 1]; byte blue = pixels[index + 2]; - byte alpha = pixels[index + 3]; + byte alpha = Model.alpha; var color = Color.FromArgb(alpha, blue, green, red); - _color_vm.result = color; + Model.result = color; } void _sample_image_mouse_down(object sender, MouseButtonEventArgs e) {