Пример #1
0
        public PresentedParameter Present(IParameterDescriptor param, Action updateCallback)
        {
            void StateChangeHandler(object sender, RoutedEventArgs args) => updateCallback();

            var checkBox = new CheckBox
            {
                HorizontalAlignment      = HorizontalAlignment.Left,
                VerticalAlignment        = VerticalAlignment.Center,
                VerticalContentAlignment = VerticalAlignment.Center
            };

            if (CheckboxTextProperty.TryGet(param.Metadata, out var checkboxText))
            {
                checkBox.Content = checkboxText;
            }
            checkBox.Checked   += StateChangeHandler;
            checkBox.Unchecked += StateChangeHandler;
            void Setter(object val) => checkBox.IsChecked = (bool?)val ?? false;
            object Getter() => checkBox.IsChecked ?? false;

            void Updater(ParameterStateType state, bool value)
            {
                switch (state)
                {
                case ParameterStateType.Enabled:
                    checkBox.IsEnabled = value;
                    break;
                }
            }

            return(new PresentedParameter(param, checkBox, new PresentedParameter.ParamDelegates(Getter, Setter, param.IsValid, Updater)));
        }
        public PresentedParameter PresentDirectly(IParameterDescriptor param, Action updateCallback)
        {
            var factory            = param.GetParameterizedObjectFactory();
            var subParameters      = factory.GetParameters(param).ToArray();
            var subParameterCount  = subParameters.Length;
            var presentedSubParams = new PresentedParameter[subParameterCount];

            var grid = new Grid();

            if (subParameterCount == 0)
            {
                grid.Children.Add(new TextBlock {
                    Text = "<EMPTY>", Foreground = Brushes.DimGray
                });
            }
            else
            {
                var labelVisible = ParamLabelVisibilityProperty.Get(param.Metadata);
                if (labelVisible)
                {
                    grid.RowDefinitions.Add(new RowDefinition {
                        Height = GridLength.Auto
                    });
                }
                grid.RowDefinitions.Add(new RowDefinition {
                    Height = GridLength.Auto
                });
                for (var i = 0; i < subParameterCount; i++)
                {
                    if (i != 0)
                    {
                        grid.ColumnDefinitions.Add(new ColumnDefinition {
                            Width = new GridLength(ViewConstants.MinorSpacing, GridUnitType.Pixel)
                        });
                    }
                    grid.ColumnDefinitions.Add(new ColumnDefinition());
                }
                for (var i = 0; i < subParameterCount; i++)
                {
                    var columnIndex = i * 2;
                    var subParam    = subParameters[i];
                    if (labelVisible)
                    {
                        var nameTextBlock = ViewHelper.CreateParamNameTextBlock(subParam);
                        nameTextBlock.FontSize      = 8;
                        nameTextBlock.TextWrapping  = TextWrapping.NoWrap;
                        nameTextBlock.TextAlignment = TextAlignment.Left;
                        grid.Children.Add(nameTextBlock);
                        Grid.SetRow(nameTextBlock, 0);
                        Grid.SetColumn(nameTextBlock, columnIndex);
                    }

                    var presentedSubParam = presentedSubParams[i] = subParam.GetPresenter().Present(subParam, updateCallback);
                    grid.Children.Add(presentedSubParam.Element);
                    if (labelVisible)
                    {
                        Grid.SetRow(presentedSubParam.Element, 1);
                    }
                    Grid.SetColumn(presentedSubParam.Element, columnIndex);

                    GridLength columnWidth;
                    if (ColumnWidthProperty.TryGet(subParam.Metadata, out var propertyValue))
                    {
                        columnWidth = propertyValue;
                    }
                    else if (presentedSubParam.Element.GetType() == typeof(CheckBox))
                    {
                        columnWidth = GridLength.Auto;
                    }
                    else
                    {
                        columnWidth = ColumnWidthProperty.DefaultValue;
                    }
                    grid.ColumnDefinitions[columnIndex].Width = columnWidth;
                }
            }

            void Setter(object obj)
            {
                var context = factory.Parse(param, (IParameterizedObject)obj);

                for (var i = 0; i < subParameters.Length; i++)
                {
                    if (context.TryGet(subParameters[i], out var val))
                    {
                        presentedSubParams[i].Delegates.Setter(val);
                    }
                }
            }

            object Getter()
            {
                var dict = new Context();

                for (var i = 0; i < subParameters.Length; i++)
                {
                    dict.Set(subParameters[i], presentedSubParams[i].Delegates.Getter());
                }
                return(factory.Create(param, dict));
            }

            bool Validator(object value)
            {
                var context = factory.Parse(param, (IParameterizedObject)value);
                var flag    = true;

                for (var i = 0; i < subParameters.Length; i++)
                {
                    var  subParam = subParameters[i];
                    bool valid;
                    try
                    {
                        var subValue = context.TryGet(subParam, out var val) ? val : subParam.DefaultValue;
                        valid = presentedSubParams[i].Delegates.Validator?.Invoke(subValue) ?? true;
                    }
                    catch (Exception)
                    {
                        valid = false;
                    }
                    presentedSubParams[i].Delegates.Updater?.Invoke(ParameterStateType.Valid, valid);
                    if (!valid)
                    {
                        flag = false;
                    }
                }
                return(flag);
            }

            void Updater(ParameterStateType state, bool value)
            {
                if (state != ParameterStateType.Enabled)
                {
                    return;
                }
                if (value)
                {
                    var context = factory.Parse(param, (IParameterizedObject)Getter());
                    for (var i = 0; i < subParameters.Length; i++)
                    {
                        presentedSubParams[i].Delegates.Updater?.Invoke(ParameterStateType.Enabled, factory.IsEnabled(context, subParameters[i]));
                    }
                }
                else
                {
                    foreach (var presentedSubParam in presentedSubParams)
                    {
                        presentedSubParam.Delegates.Updater?.Invoke(ParameterStateType.Enabled, false);
                    }
                }
            }

            return(new PresentedParameter(param, grid, Getter, Setter, Validator, Updater));
        }