using System; using System.Data; using System.Linq; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Media; using InABox.Clients; using InABox.Core; using InABox.Scripting; using Syncfusion.UI.Xaml.Grid; namespace InABox.DynamicGrid { public delegate DynamicGridStyle OnGetDynamicGridRowStyle(CoreRow row, DynamicGridStyle defaultstyle); public interface IBaseDynamicGridStyle { double FontSize { get; set; } FontStyle FontStyle { get; set; } FontWeight FontWeight { get; set; } Brush Background { get; set; } Brush Foreground { get; set; } } public interface IDynamicGridStyle : IBaseDynamicGridStyle { DependencyProperty FontSizeProperty { get; } DependencyProperty FontStyleProperty { get; } DependencyProperty FontWeightProperty { get; } DependencyProperty BackgroundProperty { get; } DependencyProperty ForegroundProperty { get; } } public abstract class DynamicGridStyle : Style, IDynamicGridStyle { public DynamicGridStyle(Type T) : base(T) { FontSize = 12D; FontWeight = FontWeights.Normal; FontStyle = FontStyles.Normal; Background = new SolidColorBrush(Colors.White); Foreground = new SolidColorBrush(Colors.Black); } public DynamicGridStyle(DynamicGridStyle source) : base(source.TargetType, source) { FontSize = source.FontSize; FontWeight = source.FontWeight; FontStyle = source.FontStyle; Background = source.Background; Foreground = source.Foreground; } public abstract DependencyProperty FontSizeProperty { get; } public abstract DependencyProperty FontStyleProperty { get; } public abstract DependencyProperty FontWeightProperty { get; } public abstract DependencyProperty BackgroundProperty { get; } public abstract DependencyProperty ForegroundProperty { get; } public double FontSize { get => Get(FontSizeProperty, 8); set => Set(FontSizeProperty, value); } public FontStyle FontStyle { get => Get(FontStyleProperty, FontStyles.Italic); set => Set(FontStyleProperty, value); } public FontWeight FontWeight { get => Get(FontWeightProperty, FontWeights.DemiBold); set => Set(FontWeightProperty, value); } public Brush Background { get => Get(BackgroundProperty, new SolidColorBrush(Colors.Yellow)); set => Set(BackgroundProperty, value); } public Brush Foreground { get => Get(ForegroundProperty, new SolidColorBrush(Colors.Green)); set => Set(ForegroundProperty, value); } private void Set(DependencyProperty property, T value) { var setter = Setters.FirstOrDefault(x => x is Setter && ((Setter)x).Property.Equals(property)) as Setter; if (setter == null) { Setters.Add(new Setter(property, value)); } else { if (!setter.IsSealed) setter.Value = value; } } private T Get(DependencyProperty property, T defaultvalue) { var setter = Setters.FirstOrDefault(x => x is Setter && ((Setter)x).Property.Equals(property)) as Setter; return setter != null ? (T)setter.Value : defaultvalue; } } public abstract class DynamicGridStyle : DynamicGridStyle { public DynamicGridStyle(IDynamicGridStyle? source) : base(typeof(T)) { if (source != null) { FontSize = source.FontSize; FontStyle = source.FontStyle; FontWeight = source.FontWeight; Background = source.Background; Foreground = source.Foreground; } } } public abstract class DynamicGridRowStyleSelector : StyleSelector, IBaseDynamicGridStyle { private static bool initialized; private static ScriptDocument? helper; private static DynamicGridStyle defaultstyle; public CoreTable Data { get; set; } public double FontSize { get => defaultstyle.FontSize; set => defaultstyle.FontSize = value; } public FontStyle FontStyle { get => defaultstyle.FontStyle; set => defaultstyle.FontStyle = value; } public FontWeight FontWeight { get => defaultstyle.FontWeight; set => defaultstyle.FontWeight = value; } public Brush Background { get => defaultstyle.Background; set => defaultstyle.Background = value; } public Brush Foreground { get => defaultstyle.Foreground; set => defaultstyle.Foreground = value; } protected abstract DynamicGridStyle CreateStyle(); private CoreRow? GetRow(object item) { try { var row = (item as DataRowBase)?.RowData as DataRowView; if (row != null) { var index = row.Row.Table.Rows.IndexOf(row.Row); return Data.Rows[index]; } return null; } catch { return null; } } private void Initialize() { if (initialized) return; defaultstyle = CreateStyle(); var stylescript = ClientFactory.ClientType == null ? null : new Client