예제 #1
0
        /// <summary>
        /// Gets a list of active bindings on the specified FrameworkElement.  Bindings are gathered
        /// according to the same conditions BindingGroup uses to find bindings of descendent elements
        /// within the visual tree.
        /// </summary>
        /// <param name="element">Root FrameworkElement to search under</param>
        /// <param name="inheritedDataContext">DomainContext of the element's parent</param>
        /// <param name="dataItem">Target DomainContext</param>
        /// <param name="twoWayOnly">If true, only returns TwoWay Bindings</param>
        /// <returns>The list of active bindings.</returns>
        private static List <DataFormBindingInfo> GetDataFormBindingInfoOfSingleElement(this FrameworkElement element, object inheritedDataContext, object dataItem, bool twoWayOnly)
        {
            // Now see which of the possible dependency properties are being used
            List <DataFormBindingInfo> bindingData = new List <DataFormBindingInfo>();

            foreach (DependencyProperty bindingTarget in ValidationUtil.GetDependencyPropertiesForElement(element))
            {
                // We add bindings according to the same conditions as BindingGroups:
                //    Element.Binding.Mode == TwoWay
                //    Element.Binding.Source == null
                //    DataItem == ContextElement.DataContext where:
                //      If Element is ContentPresenter and TargetProperty is Content, ContextElement = Element.Parent
                //      Else if TargetProperty is DomainContext, ContextElement = Element.Parent
                //      Else ContextElement = Element
                BindingExpression bindingExpression = element.GetBindingExpression(bindingTarget);
                if (bindingExpression != null &&
                    bindingExpression.ParentBinding != null &&
                    (!twoWayOnly || bindingExpression.ParentBinding.Mode == BindingMode.TwoWay) &&
                    bindingExpression.ParentBinding.Source == null)
                {
                    object dataContext;
                    if (bindingTarget == FrameworkElement.DataContextProperty ||
                        (element is ContentPresenter && bindingTarget == ContentPresenter.ContentProperty))
                    {
                        dataContext = inheritedDataContext;
                    }
                    else
                    {
                        dataContext = element.DataContext ?? inheritedDataContext;
                    }
                    if (dataItem == dataContext)
                    {
                        bindingData.Add(new DataFormBindingInfo(bindingExpression, bindingTarget, element));
                    }
                }
            }
            return(bindingData);
        }