| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Media;
- using Comal.Classes;
- using InABox.Core;
- using InABox.DynamicGrid;
- using Syncfusion.Windows.Shared;
- namespace PRS.Shared;
- public class MinimumStockEditorControl : DynamicEnclosedEditorControl<ProductInstanceMinimumStock, MinimumStockEditor>
- {
- private readonly Grid _grid = new() {
- VerticalAlignment = VerticalAlignment.Stretch,
- HorizontalAlignment = HorizontalAlignment.Stretch
- };
-
- private readonly CheckBox _customStock = new() {
- Content = "Custom",
- IsThreeState = false,
- VerticalAlignment = VerticalAlignment.Center
- };
-
- private readonly IntegerTextBox _stockLevel = new(){
- Margin = new Thickness(5, 0, 0, 0),
- TextAlignment = TextAlignment.Center,
- VerticalAlignment = VerticalAlignment.Stretch,
- };
-
- private readonly CheckBox _customUsage = new()
- {
- Content = "# Days",
- Margin = new Thickness(5, 0, 0, 0),
- IsThreeState = false,
- VerticalAlignment = VerticalAlignment.Center
- };
-
- private readonly IntegerTextBox _usageDays = new()
- {
- Margin = new Thickness(5, 0, 0, 0),
- TextAlignment = TextAlignment.Center,
- VerticalAlignment = VerticalAlignment.Stretch,
- };
-
- private readonly Label _usageFactorLabel = new()
- {
- Content = "Usage Factor",
- VerticalAlignment = VerticalAlignment.Center,
- Margin = new Thickness(5, 0, 0, 0),
- };
-
- private readonly DoubleTextBox _usageFactor = new()
- {
- Margin = new Thickness(5, 0, 0, 0),
- TextAlignment = TextAlignment.Center,
- VerticalAlignment = VerticalAlignment.Stretch,
- };
-
- private readonly Label _stockFactorLabel = new()
- {
- Content = "Stock Factor",
- VerticalAlignment = VerticalAlignment.Center,
- Margin = new Thickness(5, 0, 0, 0)
- };
-
- private readonly DoubleTextBox _stockFactor = new()
- {
- Margin = new Thickness(5, 0, 0, 0),
- TextAlignment = TextAlignment.Center,
- VerticalAlignment = VerticalAlignment.Stretch,
- };
-
- public override void Configure()
- {
- }
- protected override FrameworkElement CreateEditor()
- {
- void AddEditor(FrameworkElement element, ref int col)
- {
- element.SetValue(Grid.ColumnProperty,col);
- _grid.Children.Add(element);
- col++;
- }
- _grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Auto });
- _grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1.0, GridUnitType.Star) });
-
- _grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Auto });
- _grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1.0, GridUnitType.Star) });
-
- _grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Auto });
- _grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1.0, GridUnitType.Star) });
-
- _grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Auto });
- _grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1.0, GridUnitType.Star) });
- _grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1.0, GridUnitType.Star) });
- int col = 0;
-
- AddEditor(_customStock,ref col);
- _customStock.Checked += (o,e) => UpdateValue(nameof(ProductInstanceMinimumStock.CustomStockLevel), true);
- _customStock.Unchecked += (o,e) => UpdateValue(nameof(ProductInstanceMinimumStock.CustomStockLevel), true);
-
- AddEditor(_stockLevel,ref col);
- _stockLevel.ValueChanged += (o,e) => UpdateValue(nameof(ProductInstanceMinimumStock.StockLevel), false);
-
- AddEditor(_customUsage,ref col);
- _customUsage.Checked += (o,e) => UpdateValue(nameof(ProductInstanceMinimumStock.CustomUsage), true);
- _customUsage.Unchecked += (o,e) => UpdateValue(nameof(ProductInstanceMinimumStock.CustomUsage), true);
-
- AddEditor(_usageDays,ref col);
- _usageDays.ValueChanged += (o,e) => UpdateValue(nameof(ProductInstanceMinimumStock.UsageDays), false);
-
- AddEditor(_usageFactorLabel,ref col);
- AddEditor(_usageFactor,ref col);
- _usageFactor.ValueChanged += (o,e) => UpdateValue(nameof(ProductInstanceMinimumStock.UsageFactor), false);
- AddEditor(_stockFactorLabel,ref col);
- AddEditor(_stockFactor,ref col);
- _stockFactor.ValueChanged += (o,e) => UpdateValue(nameof(ProductInstanceMinimumStock.StockFactor), false);
- return _grid;
- }
- private void UpdateValue(string fieldname, bool checkenabled)
- {
- CheckChanged(fieldname);
- if (checkenabled)
- CheckEnabled(IsEnabled);
- }
- private void CheckEnabled(bool enabled)
- {
- _customUsage.IsEnabled = enabled;
- _usageDays.IsEnabled = enabled && _customUsage.IsChecked == true;
- _usageDays.Value = _customUsage.IsChecked != true ? 0 : (_usageDays.Value ?? 0) > 0 ? _usageDays.Value : 30;
- _usageFactor.IsEnabled = enabled && _customUsage.IsChecked == true;
- _usageFactor.Value = _customUsage.IsChecked != true ? 0 : (_usageFactor.Value ?? 0.0).IsEffectivelyGreaterThan(0.0) ? _usageFactor.Value : 1.0;
-
- _stockFactor.IsEnabled = enabled && _customUsage.IsChecked == true;
- _stockFactor.Value = _customUsage.IsChecked != true ? 0 : (_stockFactor.Value ?? 0.0).IsEffectivelyGreaterThan(0.0) ? _stockFactor.Value : 1.0;
- _customStock.IsEnabled = enabled;
-
- _stockLevel.IsEnabled = enabled && _customStock.IsChecked == true;
- _stockLevel.Value = _customStock.IsChecked != true ? 0 : (_stockLevel.Value ?? 0);
- }
- public override int DesiredWidth() => int.MaxValue;
- public override int DesiredHeight() => 30;
- public override void SetFocus() => _customUsage.Focus();
-
- public override void SetColor(Color color)
- {
- _usageDays.Background = new SolidColorBrush(color);
- _usageFactor.Background = new SolidColorBrush(color);
- _stockFactor.Background = new SolidColorBrush(color);
- _stockLevel.Background = new SolidColorBrush(color);
- }
- protected override IEnumerable<KeyValuePair<string, object?>> GetChildValues()
- {
- yield return new(nameof(ProductInstanceMinimumStock.CustomUsage), _customUsage.IsChecked ?? false);
- yield return new(nameof(ProductInstanceMinimumStock.UsageDays), (int)(_usageDays.Value ?? 0L));
- yield return new(nameof(ProductInstanceMinimumStock.UsageFactor), _usageFactor.Value ?? 0F);
- yield return new(nameof(ProductInstanceMinimumStock.StockFactor), _stockFactor.Value ?? 0F);
- yield return new(nameof(ProductInstanceMinimumStock.CustomStockLevel), _customStock.IsChecked ?? false);
- yield return new(nameof(ProductInstanceMinimumStock.StockLevel), (int)(_stockLevel.Value ?? 0L));
- }
- protected override object? GetChildValue(string property)
- {
- if (string.Equals(property,nameof(ProductInstanceMinimumStock.CustomUsage)))
- return _customUsage.IsChecked ?? false;
- if (string.Equals(property,nameof(ProductInstanceMinimumStock.UsageDays)))
- return (int)(_usageDays.Value ?? 0L);
- if (string.Equals(property,nameof(ProductInstanceMinimumStock.UsageFactor)))
- return _usageFactor.Value ?? 0F;
- if (string.Equals(property,nameof(ProductInstanceMinimumStock.StockFactor)))
- return _stockFactor.Value ?? 0F;
- if (string.Equals(property,nameof(ProductInstanceMinimumStock.CustomStockLevel)))
- return _customStock.IsChecked ?? false;
- if (string.Equals(property,nameof(ProductInstanceMinimumStock.StockLevel)))
- return (int)(_stockLevel.Value ?? 0L);
- return null;
- }
-
- protected override void SetChildValue(string property, object? value)
- {
- if (string.Equals(property,nameof(ProductInstanceMinimumStock.CustomUsage)))
- _customUsage.IsChecked = (bool)(value ?? false);
- else if(string.Equals(property,nameof(ProductInstanceMinimumStock.UsageDays)))
- _usageDays.Value = (int)(value ?? 0);
- else if (string.Equals(property,nameof(ProductInstanceMinimumStock.UsageFactor)))
- _usageFactor.Value = (double)(value ?? 0.0);
- else if (string.Equals(property,nameof(ProductInstanceMinimumStock.StockFactor)))
- _stockFactor.Value = (double)(value ?? 0.0);
- else if (string.Equals(property,nameof(ProductInstanceMinimumStock.CustomStockLevel)))
- _customStock.IsChecked = (bool)(value ?? false);
- else if (string.Equals(property,nameof(ProductInstanceMinimumStock.StockLevel)))
- _stockLevel.Value = (int)(value ?? 0);
- }
- public override void SetEnabled(bool enabled)
- {
- base.SetEnabled(enabled);
- CheckEnabled(enabled);
- }
- }
|