コード例 #1
0
        /// <summary>
        /// The method which will be called for the attached dependency property when  the new element is added to a fake collection.
        /// </summary>
        /// <param name="dependencyObject">The target FrameworkElement.</param>
        /// <param name="item">The new object to add.</param>
        public static void ProcessAddPropertyChangeEventItems(DependencyObject dependencyObject, object item)
        {
            if ((item == null) || (dependencyObject == null))
            {
                return;
            }

            if (BindHelper.IsInDesignModeStatic)
            {
                // Cannot correctly set in the design mode.
                return;
            }

            if (BindHelper.IsInDesignModeStatic)
            {
                // Cannot correctly set in the design mode.
                return;
            }

            BindEventHandlerBase bindItem = item as BindEventHandlerBase;

            if (bindItem == null)
            {
                throw new ArgumentException("AddPropertyChangeEvents accepts only BindEventHandlers Type instances ");
            }

            if (String.IsNullOrEmpty(bindItem.TargetPropertyName))
            {
                throw new ArgumentException("AddPropertyChangeEvents accepts only BindEventHandlers with set TargetPropertyName");
            }

            var listenDependencyProp = BindHelper.LocateDependencyProperty(bindItem.TargetPropertyName, dependencyObject);

            if (listenDependencyProp == null)
            {
                throw new ArgumentException("AddPropertyChangeEvents cannot obtain proper Type of TargetPropertyName = " + bindItem.TargetPropertyName);
            }

            DependencyPropertyDescriptor dependencyPropertyDescriptor = DependencyPropertyDescriptor.FromProperty(listenDependencyProp, listenDependencyProp.OwnerType);

            if (dependencyPropertyDescriptor == null)
            {
                throw new ArgumentException("AddPropertyChangeEvents cannot obtain proper Descriptor of TargetPropertyName = " + bindItem.TargetPropertyName);
            }

            var evInfo = BindHelper.GetEventInfo(typeof(BindXAML), "_localTypeStub");

            ProvideValueTarget provideValueTarget = new ProvideValueTarget(dependencyObject, evInfo);
            ServiceProvider    serviceProvider    = new ServiceProvider(provideValueTarget);
            var result = bindItem.ProvideValue(serviceProvider);

            if (result != null)
            {
                dependencyPropertyDescriptor.AddValueChanged(dependencyObject, (EventHandler)result);
            }
        }
コード例 #2
0
        /// <summary>
        /// The method which will be called for the attached dependency property when  the new element is added to a fake collection.
        /// </summary>
        /// <param name="dependencyObject">The target FrameworkElement.</param>
        /// <param name="item">The new object to add.</param>
        public static void ProcessAddEventItems(DependencyObject dependencyObject, object item)
        {
            if ((item == null) || (dependencyObject == null))
            {
                return;
            }

            UIElement uiElement = dependencyObject as UIElement;

            if (uiElement == null)
            {
                return;
            }
            if (BindHelper.IsInDesignModeStatic)
            {
                // Cannot correctly set in the design mode.
                return;
            }

            BindEventHandlerBase bindItem = item as BindEventHandlerBase;

            if (bindItem == null)
            {
                throw new ArgumentException("AddEvents accepts only BindEventHandlers Type instances ");
            }

            if (String.IsNullOrEmpty(bindItem.TargetEventName))
            {
                throw new ArgumentException("AddEvents accepts only BindEventHandlers with set TargetEventName");
            }

            var evntInfo = uiElement.GetEventInfo(bindItem.TargetEventName);

            if (evntInfo == null)
            {
                throw new ArgumentException("AddEvents cannot resolve TargetEventName = " + bindItem.TargetEventName);
            }

            ProvideValueTarget provideValueTarget = new ProvideValueTarget(uiElement, evntInfo);
            ServiceProvider    serviceProvider    = new ServiceProvider(provideValueTarget);
            var result = bindItem.ProvideValue(serviceProvider);

            if (result != null)
            {
#if WINDOWS_UWP
                evntInfo.AddMethod.Invoke(uiElement, new object[] { result });
#else
                evntInfo.AddEventHandler(uiElement, (Delegate)result);
#endif
            }
        }