Пример #1
0
        /// <summary>
        /// Binds to the specified child <paramref name="binding"/> of this binding.
        /// </summary>
        /// <remarks>
        /// This can be used to bind to child objects of your view model, for example
        /// <code>model.SomeProperty.ChildProperty</code>.
        /// </remarks>
        /// <example>
        /// Use this like so:
        /// <code>
        ///     public class MyChild { public SomeChildProperty { get; set; } }
        ///     public class MyModel { public ChildObject { get; set; } }
        ///
        ///     var model = new MyModel();
        ///     Binding.Property(model, (MyModel m) => m.ChildObject).Child(Binding.Property("SomeChildProperty"));
        /// </code>
        /// </example>
        /// <returns>The binding to the child property accessed through the current binding.</returns>
        /// <param name="binding">Binding to get the child value from this binding.</param>
        /// <typeparam name="TNewValue">The type of the child property value.</typeparam>
        public new BindableBinding <T, TNewValue> Child <TNewValue>(IndirectBinding <TNewValue> binding)
        {
            object childBindingReference          = null;
            EventHandler <EventArgs> eventHandler = null;
            EventHandler <EventArgs> valueChanged = (sender, e) =>
            {
                binding.RemoveValueChangedHandler(childBindingReference, eventHandler);
                eventHandler?.Invoke(sender, e);
                childBindingReference = binding.AddValueChangedHandler(DataValue, eventHandler);
            };

            return(new BindableBinding <T, TNewValue>(
                       DataItem,
                       c => binding.GetValue(DataValue),
                       (c, v) => binding.SetValue(DataValue, v),
                       addChangeEvent: (c, ev) =>
            {
                eventHandler = ev;
                DataValueChanged += valueChanged;
                childBindingReference = binding.AddValueChangedHandler(DataValue, ev);
            },
                       removeChangeEvent: (c, ev) =>
            {
                binding.RemoveValueChangedHandler(childBindingReference, ev);
                DataValueChanged -= valueChanged;
            }
                       ));
        }
Пример #2
0
        /// <summary>
        /// Binds to the specified child <paramref name="binding"/> of this binding.
        /// </summary>
        /// <remarks>
        /// This can be used to bind to child objects of your view model, for example
        /// <code>model.SomeProperty.ChildProperty</code>.
        /// </remarks>
        /// <example>
        /// Use this like so:
        /// <code>
        ///     public class MyChild { public SomeChildProperty { get; set; } }
        ///     public class MyModel { public ChildObject { get; set; } }
        ///
        ///     var model = new MyModel();
        ///     Binding.Property(model, (MyModel m) => m.ChildObject).Child(Binding.Property("SomeChildProperty"));
        /// </code>
        /// </example>
        /// <returns>The binding to the child property accessed through the current binding.</returns>
        /// <param name="binding">Binding to get the child value from this binding.</param>
        /// <typeparam name="TValue">The type of the child property value.</typeparam>
        public DirectBinding <TValue> Child <TValue>(IndirectBinding <TValue> binding)
        {
            object childBindingReference          = null;
            EventHandler <EventArgs> eventHandler = null;
            EventHandler <EventArgs> valueChanged = (sender, e) =>
            {
                binding.RemoveValueChangedHandler(childBindingReference, eventHandler);
                eventHandler?.Invoke(sender, e);
                childBindingReference = binding.AddValueChangedHandler(DataValue, eventHandler);
            };

            return(new DelegateBinding <TValue>(
                       () => binding.GetValue(DataValue),
                       v => binding.SetValue(DataValue, v),
                       addChangeEvent: ev =>
            {
                eventHandler = ev;
                DataValueChanged += valueChanged;
                childBindingReference = binding.AddValueChangedHandler(DataValue, ev);
            },
                       removeChangeEvent: ev =>
            {
                binding.RemoveValueChangedHandler(childBindingReference, ev);
                DataValueChanged -= valueChanged;
            }
                       ));
        }
Пример #3
0
        /// <summary>
        /// Binds to the specified child <paramref name="binding"/> of this binding.
        /// </summary>
        /// <remarks>
        /// This can be used to bind to child objects of your view model, for example
        /// <code>model.SomeProperty.ChildProperty</code>.
        /// </remarks>
        /// <example>
        /// Use this like so:
        /// <code>
        ///     public class MyChild { public SomeChildProperty { get; set; } }
        ///     public class MyModel { public ChildObject { get; set; } }
        ///
        ///     Binding.Property((MyModel m) => m.ChildObject).Child(Binding.Property("SomeChildProperty"));
        /// </code>
        /// </example>
        /// <returns>The binding to the child property accessed through the current binding.</returns>
        /// <param name="binding">Binding to get the child value from this binding.</param>
        /// <typeparam name="TNewValue">The type of the child property value.</typeparam>
        public IndirectBinding <TNewValue> Child <TNewValue>(IndirectBinding <TNewValue> binding)
        {
            object bindingReference      = null;
            object childBindingReference = null;
            object context = null;
            EventHandler <EventArgs> eventHandler = null;
            EventHandler <EventArgs> valueChanged = (sender, e) =>
            {
                binding.RemoveValueChangedHandler(childBindingReference, eventHandler);
                eventHandler?.Invoke(sender, e);
                childBindingReference = binding.AddValueChangedHandler(GetValue(context), eventHandler);
            };

            return(new DelegateBinding <object, TNewValue>(
                       c => binding.GetValue(GetValue(context = c)),
                       (c, v) => binding.SetValue(GetValue(context = c), v),
                       addChangeEvent: (c, ev) =>
            {
                context = c;
                eventHandler = ev;
                bindingReference = AddValueChangedHandler(c, valueChanged);

                childBindingReference = binding.AddValueChangedHandler(GetValue(c), ev);
            },
                       removeChangeEvent: (c, ev) =>
            {
                binding.RemoveValueChangedHandler(childBindingReference, ev);
                RemoveValueChangedHandler(bindingReference, valueChanged);
            }
                       ));
        }
Пример #4
0
 /// <inheritdoc/>
 public override void RemoveValueChangedHandler(object bindingReference, EventHandler <EventArgs> handler)
 {
     if (bindingReference is BindingReference reference)
     {
         _parent.RemoveValueChangedHandler(reference.parentReference, reference.ValueChanged);
         _child.RemoveValueChangedHandler(reference.childReference, handler);
     }
 }
Пример #5
0
        /// <summary>
        /// Binds to the specified child <paramref name="binding"/> of this binding.
        /// </summary>
        /// <remarks>
        /// This can be used to bind to child objects of your view model, for example
        /// <code>model.SomeProperty.ChildProperty</code>.
        /// </remarks>
        /// <example>
        /// Use this like so:
        /// <code>
        ///     public class MyChild { public SomeChildProperty { get; set; } }
        ///     public class MyModel { public ChildObject { get; set; } }
        ///
        ///     var model = new MyModel();
        ///     Binding.Property(model, (MyModel m) => m.ChildObject).Child(Binding.Property("SomeChildProperty"));
        /// </code>
        /// </example>
        /// <returns>The binding to the child property accessed through the current binding.</returns>
        /// <param name="binding">Binding to get the child value from this binding.</param>
        /// <typeparam name="TValue">The type of the child property value.</typeparam>
        public DirectBinding <TValue> Child <TValue>(IndirectBinding <TValue> binding)
        {
            object childBindingReference          = null;
            EventHandler <EventArgs> eventHandler = null;

            void valueChanged(object sender, EventArgs e)
            {
                binding.RemoveValueChangedHandler(childBindingReference, eventHandler);
                eventHandler?.Invoke(sender, e);
                childBindingReference = binding.AddValueChangedHandler(DataValue, eventHandler);
            }

            void setValueStruct(TValue v)
            {
                object parentValue = DataValue;

                binding.SetValue(parentValue, v);
                DataValue = (T)parentValue;
            }

            void setValueObject(TValue v) => binding.SetValue(DataValue, v);

            var isStruct = typeof(T).GetTypeInfo().IsValueType;

            return(new DelegateBinding <TValue>(
                       () => binding.GetValue(DataValue),
                       isStruct ? (Action <TValue>)setValueStruct : setValueObject,
                       addChangeEvent: ev =>
            {
                eventHandler = ev;
                DataValueChanged += valueChanged;
                childBindingReference = binding.AddValueChangedHandler(DataValue, ev);
            },
                       removeChangeEvent: ev =>
            {
                binding.RemoveValueChangedHandler(childBindingReference, ev);
                DataValueChanged -= valueChanged;
            }
                       ));
        }