/// <summary>
        /// Binds a nullable enum scalar data to <see cref="CheckBox"/>.
        /// </summary>
        /// <typeparam name="T">The enum type.</typeparam>
        /// <param name="source">The source scalar data.</param>
        /// <param name="enumMemberValue">The value of enum member.</param>
        /// <param name="title">The title of the CheckBox. If null, the name of enum member will be used.</param>
        /// <returns>The scalar binding object.</returns>
        public static ScalarBinding <CheckBox> BindToCheckBox <T>(this Scalar <T?> source, T enumMemberValue, object title = null)
            where T : struct, IConvertible
        {
            if (!typeof(T).IsEnum)
            {
                throw new ArgumentException(DiagnosticMessages.BindingFactory_EnumTypeRequired(nameof(T)), nameof(T));
            }
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            return(new ScalarBinding <CheckBox>(onRefresh: v => v.IsChecked = source.GetValue().HasFlag(enumMemberValue),
                                                onSetup: v =>
            {
                v.Content = title ?? Enum.GetName(typeof(T), enumMemberValue);
            }, onCleanup: null)
                   .BeginInput(new PropertyChangedTrigger <CheckBox>(CheckBox.IsCheckedProperty), new ExplicitTrigger <CheckBox>())
                   .WithFlush(source, v =>
            {
                var value = source.GetValue();
                var newValue = value.GetNewValue(enumMemberValue, v, true);
                if (Comparer <T?> .Default.Compare(value, newValue) == 0)
                {
                    return false;
                }
                source.EditValue(newValue);
                return true;
            })
                   .EndInput());
        }
Example #2
0
        /// <summary>
        /// Setup the flushing operation.
        /// </summary>
        /// <typeparam name="TData">Data type of scalar data.</typeparam>
        /// <param name="scalar">The scalar data.</param>
        /// <param name="getValue">The delegate to return data value.</param>
        /// <returns>This scalar input for fluent coding.</returns>
        public ScalarInput <T> WithFlush <TData>(Scalar <TData> scalar, Func <T, TData> getValue)
        {
            if (scalar == null)
            {
                throw new ArgumentNullException(nameof(scalar));
            }

            VerifyNotSealed();
            _target = _target.Union(scalar);
            _flushFuncs.Add(element =>
            {
                if (getValue == null)
                {
                    return(false);
                }
                var value = getValue(element);
                return(scalar.EditValue(value));
            });
            return(this);
        }