|
@@ -1,10 +1,12 @@
|
|
|
using System;
|
|
|
using System.Collections.Generic;
|
|
|
+using System.Globalization;
|
|
|
using System.Linq;
|
|
|
using System.Windows;
|
|
|
using System.Windows.Controls;
|
|
|
using System.Windows.Data;
|
|
|
using System.Windows.Media;
|
|
|
+using System.Windows.Media.Imaging;
|
|
|
using InABox.Core;
|
|
|
using InABox.WPF;
|
|
|
using Microsoft.Xaml.Behaviors;
|
|
@@ -12,16 +14,47 @@ using Syncfusion.UI.Xaml.Grid;
|
|
|
|
|
|
namespace InABox.DynamicGrid;
|
|
|
|
|
|
+public class DynamicGridMaskColumnButtonClickArgs
|
|
|
+{
|
|
|
+ public object? Value { get; set; }
|
|
|
+ public DynamicGridMaskColumnButtonClickArgs(object? value)
|
|
|
+ {
|
|
|
+ Value = value;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+public enum DynamicGridMaskColumnButtonPosition
|
|
|
+{
|
|
|
+ Left,
|
|
|
+ Right
|
|
|
+}
|
|
|
+
|
|
|
+public class DynamicGridMaskColumnButton
|
|
|
+{
|
|
|
+ public BitmapImage? Image { get; set; }
|
|
|
+
|
|
|
+ public Visibility Visibility { get; set; }
|
|
|
+
|
|
|
+ public DynamicGridMaskColumnButtonPosition Position { get; set; } = DynamicGridMaskColumnButtonPosition.Right;
|
|
|
+
|
|
|
+ public Action<DynamicGridMaskColumnButton,DynamicGridMaskColumnButtonClickArgs>? Clicked { get; set; }
|
|
|
+
|
|
|
+ public object? DoClick(object? value)
|
|
|
+ {
|
|
|
+ var args = new DynamicGridMaskColumnButtonClickArgs(value);
|
|
|
+ Clicked?.Invoke(this, args);
|
|
|
+ return args.Value;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
public abstract class DynamicGridMaskColumn<TEntity, TEditor> : DynamicGridEditorColumn<TEntity, TEditor, GridTemplateColumn>
|
|
|
where TEntity : BaseObject
|
|
|
where TEditor : class,new()
|
|
|
{
|
|
|
|
|
|
- protected abstract Behavior CreateBehaviour();
|
|
|
-
|
|
|
- protected abstract IValueConverter CreateConverter();
|
|
|
+ protected abstract Behavior? CreateBehaviour();
|
|
|
|
|
|
- protected abstract Button[] CreateButtons(TextBox textbox);
|
|
|
+ protected abstract IValueConverter? CreateConverter();
|
|
|
|
|
|
protected override void Configure(GridTemplateColumn column, TEditor editor)
|
|
|
{
|
|
@@ -30,12 +63,15 @@ public abstract class DynamicGridMaskColumn<TEntity, TEditor> : DynamicGridEdito
|
|
|
(
|
|
|
() =>
|
|
|
{
|
|
|
- var result = new Label();
|
|
|
- result.HorizontalContentAlignment = Column.TextAlignment == TextAlignment.Left
|
|
|
- ? HorizontalAlignment.Left
|
|
|
- : Column.TextAlignment == TextAlignment.Center
|
|
|
- ? HorizontalAlignment.Center
|
|
|
- : HorizontalAlignment.Right;
|
|
|
+ var result = new Label
|
|
|
+ {
|
|
|
+ HorizontalContentAlignment = Column.TextAlignment == TextAlignment.Left
|
|
|
+ ? HorizontalAlignment.Left
|
|
|
+ : Column.TextAlignment == TextAlignment.Center
|
|
|
+ ? HorizontalAlignment.Center
|
|
|
+ : HorizontalAlignment.Right,
|
|
|
+ VerticalContentAlignment = VerticalAlignment.Center
|
|
|
+ };
|
|
|
var binding = new Binding()
|
|
|
{
|
|
|
Path = new PropertyPath(MappingName),
|
|
@@ -50,60 +86,136 @@ public abstract class DynamicGridMaskColumn<TEntity, TEditor> : DynamicGridEdito
|
|
|
(
|
|
|
() =>
|
|
|
{
|
|
|
+ var border = new Border()
|
|
|
+ {
|
|
|
+ BorderBrush = new SolidColorBrush(Colors.Gray),
|
|
|
+ BorderThickness = new Thickness(0.75),
|
|
|
+ Padding = new Thickness(0),
|
|
|
+ Margin = new Thickness(0)
|
|
|
+ };
|
|
|
+
|
|
|
+ var dock = new DockPanel();
|
|
|
+ dock.SourceUpdated += OnSourceUpdated;
|
|
|
+ dock.TargetUpdated += OnTargetUpdated;
|
|
|
+ border.Child = dock;
|
|
|
|
|
|
- var result = new Grid();
|
|
|
- result.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Star)});
|
|
|
- result.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Auto)});
|
|
|
+ var textbox = new TextBox
|
|
|
+ {
|
|
|
+ CharacterCasing = CharacterCasing.Upper,
|
|
|
+ TextAlignment = Column.TextAlignment,
|
|
|
+ BorderThickness = new Thickness(0),
|
|
|
+ //Padding = new Thickness(2, 0, 0, 0),
|
|
|
+ VerticalContentAlignment = VerticalAlignment.Center,
|
|
|
+ Background = new SolidColorBrush(Colors.White)
|
|
|
+ };
|
|
|
|
|
|
- var textbox = new TextBox();
|
|
|
- textbox.CharacterCasing = CharacterCasing.Upper;
|
|
|
- textbox.TextAlignment = Column.TextAlignment;
|
|
|
textbox.SetBinding(TextBox.TextProperty, new Binding()
|
|
|
{
|
|
|
Path = new PropertyPath(MappingName),
|
|
|
- Converter = converter
|
|
|
+ Converter = converter,
|
|
|
+ NotifyOnSourceUpdated = true,
|
|
|
+ NotifyOnTargetUpdated = true,
|
|
|
+
|
|
|
});
|
|
|
|
|
|
- textbox.SetValue(Grid.ColumnSpanProperty, 2);
|
|
|
- textbox.Padding = new Thickness(2, 0, 0, 0);
|
|
|
- textbox.VerticalContentAlignment = VerticalAlignment.Center;
|
|
|
+ var behaviour = CreateBehaviour();
|
|
|
+ if (behaviour != null)
|
|
|
+ Interaction.GetBehaviors(textbox).Add(behaviour);
|
|
|
+
|
|
|
textbox.SetValue(FocusManagerHelper.FocusedElementProperty, true);
|
|
|
-
|
|
|
- Interaction.GetBehaviors(textbox).Add(CreateBehaviour());
|
|
|
+ textbox.SetValue(DockPanel.DockProperty,Dock.Left);
|
|
|
|
|
|
- result.Children.Add(textbox);
|
|
|
-
|
|
|
- var padding = 0.0;
|
|
|
- var buttons = CreateButtons(textbox);
|
|
|
- if (buttons?.Any() == true)
|
|
|
+ var defs = CreateButtons();
|
|
|
+ if (defs?.Any() == true)
|
|
|
{
|
|
|
- StackPanel stack = new StackPanel()
|
|
|
+ foreach (var def in defs)
|
|
|
{
|
|
|
- Orientation = Orientation.Horizontal
|
|
|
-
|
|
|
- };
|
|
|
- stack.SetValue(Grid.ColumnProperty,1);
|
|
|
- result.Children.Add(stack);
|
|
|
-
|
|
|
- foreach (var button in buttons)
|
|
|
- {
|
|
|
- button.Margin = new Thickness(1);
|
|
|
- button.BorderThickness = new Thickness(0.75, 0, 0, 0);
|
|
|
- stack.Children.Add(button);
|
|
|
- padding += button.Width;
|
|
|
+ Button button = new Button()
|
|
|
+ {
|
|
|
+ Tag = def,
|
|
|
+ Content = new Image() { Source = def.Image },
|
|
|
+ Padding = new Thickness(2),
|
|
|
+ Width = 25,
|
|
|
+ BorderThickness = new Thickness(0),
|
|
|
+ Background = new SolidColorBrush(Colors.White),
|
|
|
+ Visibility = def.Visibility,
|
|
|
+ };
|
|
|
+ button.Click += (sender, args) =>
|
|
|
+ {
|
|
|
+ var cvt = CreateConverter();
|
|
|
+ var val = cvt != null
|
|
|
+ ? cvt.ConvertBack(textbox.Text, typeof(object), null, CultureInfo.CurrentCulture)
|
|
|
+ : textbox.Text;
|
|
|
+ val = def.DoClick(val);
|
|
|
+ textbox.Text = cvt != null
|
|
|
+ ? cvt.Convert(val, typeof(String), null, CultureInfo.CurrentCulture) as String ?? ""
|
|
|
+ : val?.ToString() ?? "";
|
|
|
+ textbox.GetBindingExpression(TextBox.TextProperty)?.UpdateSource();
|
|
|
+ button.Content = new Image() { Source = def.Image };
|
|
|
+ button.Visibility = def.Visibility;
|
|
|
+ };
|
|
|
+ button.SetValue(DockPanel.DockProperty,def.Position == DynamicGridMaskColumnButtonPosition.Left ? Dock.Left : Dock.Right);
|
|
|
+ dock.Children.Add(button);
|
|
|
}
|
|
|
-
|
|
|
}
|
|
|
-
|
|
|
- textbox.Padding = new Thickness(0, 0, padding, 0);
|
|
|
|
|
|
- return result;
|
|
|
+ dock.Children.Add(textbox);
|
|
|
+
|
|
|
+ return border;
|
|
|
}
|
|
|
|
|
|
);
|
|
|
column.SetCellBoundValue = false;
|
|
|
}
|
|
|
|
|
|
+ protected virtual DynamicGridMaskColumnButton[]? CreateButtons()
|
|
|
+ {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void OnTargetUpdated(object? sender, DataTransferEventArgs e)
|
|
|
+ {
|
|
|
+ if (sender is DockPanel dock && e.Source is TextBox textbox && e.Property == TextBox.TextProperty)
|
|
|
+ {
|
|
|
+ var buttons = dock?.Children.OfType<Button>().ToArray();
|
|
|
+ var defs = buttons?.Select(x => x.Tag).OfType<DynamicGridMaskColumnButton>()?.ToArray();
|
|
|
+ if (defs?.Any() == true)
|
|
|
+ {
|
|
|
+ var cvt = CreateConverter();
|
|
|
+ var val = cvt != null
|
|
|
+ ? cvt.ConvertBack(textbox.Text, typeof(object), null, CultureInfo.CurrentCulture)
|
|
|
+ : textbox.Text;
|
|
|
+ UpdateButtons(val, defs);
|
|
|
+ if (buttons != null)
|
|
|
+ foreach (var button in buttons)
|
|
|
+ {
|
|
|
+ var def = button.Tag as DynamicGridMaskColumnButton;
|
|
|
+ button.Content = new Image() { Source = def?.Image };
|
|
|
+ button.Visibility = def?.Visibility ?? Visibility.Collapsed;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ protected virtual void UpdateButtons(object? value, DynamicGridMaskColumnButton[]? buttons)
|
|
|
+ {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ private void OnSourceUpdated(object? sender, DataTransferEventArgs e)
|
|
|
+ {
|
|
|
+ if (sender is DockPanel dock && e.Source is TextBox textbox && e.Property == TextBox.TextProperty)
|
|
|
+ {
|
|
|
+ var buttons = dock?.Children.OfType<Button>().ToArray();
|
|
|
+ SourceUpdated(textbox.Text, buttons ?? new Button[] { });
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ protected virtual void SourceUpdated(string text, Button[] buttons)
|
|
|
+ {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
protected DynamicGridMaskColumn(DynamicGridColumn definition) : base(definition)
|
|
|
{
|
|
|
}
|