123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280 |
- using System;
- using System.Data;
- using System.Linq;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Media;
- using InABox.Clients;
- using InABox.Core;
- using InABox.Scripting;
- using Syncfusion.UI.Xaml.Grid;
- namespace InABox.DynamicGrid
- {
- public delegate DynamicGridStyle OnGetDynamicGridStyle(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<double>(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<Brush>(BackgroundProperty, new SolidColorBrush(Colors.Yellow));
- set => Set(BackgroundProperty, value);
- }
- public Brush Foreground
- {
- get => Get<Brush>(ForegroundProperty, new SolidColorBrush(Colors.Green));
- set => Set(ForegroundProperty, value);
- }
- private void Set<T>(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<T>(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<T> : 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 DynamicGridStyleSelector<T> : 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.GetClientType() == null
- ? null
- : new Client<Script>()
- .Load(new Filter<Script>(x => x.Section).IsEqualTo(typeof(T).EntityName()).And(x => x.ScriptType).IsEqualTo(ScriptType.RowStyle))
- .FirstOrDefault();
- if (stylescript != null)
- {
- helper = new ScriptDocument(stylescript.Code);
- if (!helper.Compile())
- helper = null;
- }
- initialized = true;
- }
- public event OnGetDynamicGridStyle GetStyle;
- private DynamicGridStyle ExecuteHelper(CoreRow row, DynamicGridStyle style)
- {
- var result = style;
- try
- {
- helper.SetValue("Row", row);
- helper.SetValue("Background", style.Background);
- helper.SetValue("Foreground", style.Foreground);
- helper.SetValue("Style", style.FontStyle);
- helper.SetValue("Weight", style.FontWeight);
- helper.SetValue("Size", style.FontSize);
- if (helper.Execute())
- {
- result = CreateStyle();
- result.Background = (Brush)helper.GetValue("Background");
- result.Foreground = (Brush)helper.GetValue("Foreground");
- result.FontStyle = (FontStyle)helper.GetValue("Style");
- result.FontWeight = (FontWeight)helper.GetValue("Weight");
- result.FontSize = (double)helper.GetValue("Size");
- }
- }
- catch (Exception e)
- {
- //MessageBox.Show("Unable to Invoke Row Style Helper!\r\n\r\n" + e.Message);
- }
- return result;
- }
- public override Style SelectStyle(object item, DependencyObject container)
- {
- Initialize();
- if (Data == null)
- return defaultstyle;
- if (item == null)
- return defaultstyle;
- var row = GetRow(item);
- if (row == null)
- return defaultstyle;
- var style = GetStyle != null ? GetStyle(row, defaultstyle) : defaultstyle;
- if (helper != null)
- return ExecuteHelper(row, style);
- return style;
- }
- }
- public class DynamicGridStyleSelector<TEntity, TStyle> : DynamicGridStyleSelector<TEntity> where TStyle : DynamicGridStyle, new()
- {
- protected override DynamicGridStyle CreateStyle()
- {
- return new TStyle();
- }
- }
- }
|