当前位置: 首页>>代码示例>>C#>>正文


C# FrameworkElement.GetBindingExpression方法代码示例

本文整理汇总了C#中FrameworkElement.GetBindingExpression方法的典型用法代码示例。如果您正苦于以下问题:C# FrameworkElement.GetBindingExpression方法的具体用法?C# FrameworkElement.GetBindingExpression怎么用?C# FrameworkElement.GetBindingExpression使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在FrameworkElement的用法示例。


在下文中一共展示了FrameworkElement.GetBindingExpression方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: ParseMetadata

        /// <summary>
        /// Parse metadata from a target FrameworkElement.  This will cache the metadata on the element as an attached property.
        /// </summary>
        /// <param name="element">The target FrameworkElement to pull metadata from.</param>
        /// <param name="forceUpdate">If set, will not pull metadata from cache.</param>
        /// <param name="entity">The entity used.</param>
        /// <param name="bindingExpression">The bindingExpression used.</param>
        /// <returns>Returns the metadata associated with the element.  Will be null if no metadata was found.</returns>
        internal static ValidationMetadata ParseMetadata(FrameworkElement element, bool forceUpdate, out object entity, out BindingExpression bindingExpression)
        {
            entity = null;
            bindingExpression = null;
            if (element == null)
            {
                return null;
            }

            if (!forceUpdate)
            {
                ValidationMetadata existingVMD = element.GetValue(ValidationMetadataProperty) as ValidationMetadata;
                if (existingVMD != null)
                {
                    return existingVMD;
                }
            }

            BindingExpression be = null;
            FieldInfo[] fields = element.GetType().GetFields(BindingFlags.Static | BindingFlags.Public | BindingFlags.FlattenHierarchy);
            foreach (FieldInfo field in fields)
            {
                if (field.FieldType == typeof(DependencyProperty))
                {
                    // Found a dependency property
                    be = element.GetBindingExpression((DependencyProperty)field.GetValue(null));
                    if (be != null && be.ParentBinding != null && be.ParentBinding.Path != null)
                    {
                        // Found a BindingExpression, ensure it has valid data
                        entity = be.DataItem != null ? be.DataItem : element.DataContext;
                        if (entity != null)
                        {
                            if (be.ParentBinding.Mode == BindingMode.TwoWay)
                            {
                                bindingExpression = be;
                                // A twoway binding will be automatically chosen and the rest ignored
                                break;
                            }

                            // Perform an arbitrary sort on path (string), so the same dependency property is chosen consistently.
                            // Reflection ordering is not deterministic and if we just pick the first, we could be
                            // matched with different dependency properties depending on the run.
                            if (bindingExpression == null || string.Compare(be.ParentBinding.Path.Path, bindingExpression.ParentBinding.Path.Path, StringComparison.Ordinal) < 0)
                            {
                                bindingExpression = be;
                            }
                        }
                    }
                }
            }
            if (bindingExpression != null)
            {
                ValidationMetadata newVMD = ParseMetadata(bindingExpression.ParentBinding.Path.Path, entity);
                element.SetValue(ValidationMetadataProperty, newVMD);
                return newVMD;
            }
            return null;
        }
开发者ID:expanz,项目名称:expanz-Microsoft-XAML-SDKs,代码行数:66,代码来源:ValidationHelper.cs

示例2: CreateBindings

 internal override List<BindingInfo> CreateBindings(FrameworkElement element, object dataItem, bool twoWay)
 {
     BindingInfo bindingData = new BindingInfo();
     if (twoWay && this.BindingTarget != null)
     {
         bindingData.BindingExpression = element.GetBindingExpression(this.BindingTarget);
         if (bindingData.BindingExpression != null)
         {
             bindingData.BindingTarget = this.BindingTarget;
             bindingData.Element = element;
             return new List<BindingInfo> { bindingData };
         }
     }
     foreach (DependencyProperty bindingTarget in element.GetDependencyProperties(false))
     {
         bindingData.BindingExpression = element.GetBindingExpression(bindingTarget);
         if (bindingData.BindingExpression != null
             && bindingData.BindingExpression.ParentBinding == this.Binding)
         {
             this.BindingTarget = bindingTarget;
             bindingData.BindingTarget = this.BindingTarget;
             bindingData.Element = element;
             return new List<BindingInfo> { bindingData };
         }
     }
     return base.CreateBindings(element, dataItem, twoWay);
 }
开发者ID:kvervo,项目名称:HorizontalLoopingSelector,代码行数:27,代码来源:DataGridBoundColumn.cs

示例3: UpdateBindingsOnElement

        /// <summary>
        /// Finds any bindings on an element and updates the ones in which Mode is TwoWay
        /// to set the two Boolean properties to true.
        /// </summary>
        /// <param name="element">The element.</param>
        private void UpdateBindingsOnElement(FrameworkElement element)
        {
            if (this.DataContext == null)
            {
                return;
            }

            // DataFields will run themselves, so don't bother if we're looking at a DataField.
            if (!(element is DataField))
            {
                List<DependencyProperty> dependencyProperties = ValidationUtil.GetDependencyPropertiesForElement(element);
                Debug.Assert(dependencyProperties != null, "GetDependencyPropertiesForElement() should never return null.");

                foreach (DependencyProperty dependencyProperty in dependencyProperties)
                {
                    if (dependencyProperty != null)
                    {
                        BindingExpression bindingExpression = element.GetBindingExpression(dependencyProperty);

                        if (bindingExpression != null && bindingExpression.ParentBinding != null)
                        {
                            Binding binding = ValidationUtil.CopyBinding(bindingExpression.ParentBinding);

                            if (binding.Path != null && !String.IsNullOrEmpty(binding.Path.Path) && binding.Mode == BindingMode.TwoWay)
                            {
                                PropertyInfo propertyInfo = this.DataContext.GetType().GetPropertyInfo(binding.Path.Path);

                                if (propertyInfo == null || !propertyInfo.CanWrite)
                                {
                                    // Ignore bindings to nonexistent or read-only properties.
                                    continue;
                                }

                                binding.ValidatesOnExceptions = true;
                                binding.NotifyOnValidationError = true;
                            }

                            if (binding.Converter == null)
                            {
                                binding.Converter = new DataFormValueConverter();
                            }

                            element.SetBinding(dependencyProperty, binding);

                            TextBox textBox = element as TextBox;

                            if (textBox != null)
                            {
                                if (binding.UpdateSourceTrigger != UpdateSourceTrigger.Explicit)
                                {
                                    this._lostFocusFired[textBox] = true;

                                    textBox.LostFocus -= new RoutedEventHandler(this.OnTextBoxLostFocus);
                                    textBox.LostFocus += new RoutedEventHandler(this.OnTextBoxLostFocus);
                                    textBox.TextChanged -= new TextChangedEventHandler(this.OnTextBoxTextChanged);
                                    textBox.TextChanged += new TextChangedEventHandler(this.OnTextBoxTextChanged);
                                }
                                else
                                {
                                    if (this._lostFocusFired.ContainsKey(textBox))
                                    {
                                        this._lostFocusFired.Remove(textBox);
                                    }

                                    textBox.LostFocus -= new RoutedEventHandler(this.OnTextBoxLostFocus);
                                    textBox.TextChanged -= new TextChangedEventHandler(this.OnTextBoxTextChanged);
                                }
                            }
                        }
                    }
                }

                int childrenCount = VisualTreeHelper.GetChildrenCount(element);

                for (int i = 0; i < childrenCount; i++)
                {
                    FrameworkElement childElement = VisualTreeHelper.GetChild(element, i) as FrameworkElement;

                    // Stop if we've found a child DataForm.
                    if (childElement != null && childElement.GetType() != typeof(DataForm))
                    {
                        this.UpdateBindingsOnElement(childElement);
                    }
                }
            }
        }
开发者ID:modulexcite,项目名称:SilverlightToolkit,代码行数:91,代码来源:DataField.cs

示例4: UpdateSourceOnElementBindings

        /// <summary>
        /// Updates the source on the bindings for a given element.
        /// </summary>
        /// <param name="element">The element.</param>
        public static void UpdateSourceOnElementBindings(FrameworkElement element)
        {
            List<DependencyProperty> dependencyProperties = GetDependencyPropertiesForElement(element);
            Debug.Assert(dependencyProperties != null, "GetDependencyPropertiesForElement() should never return null.");

            foreach (DependencyProperty dependencyProperty in dependencyProperties)
            {
                if (dependencyProperty != null)
                {
                    BindingExpression bindingExpression = element.GetBindingExpression(dependencyProperty);

                    if (bindingExpression != null)
                    {
                        bindingExpression.UpdateSource();
                    }
                }
            }
        }
开发者ID:modulexcite,项目名称:SilverlightToolkit,代码行数:22,代码来源:ValidationUtil.cs

示例5: GetBindingExpressionsForElement

        /// <summary>
        /// Gets the list of binding expressions for the given element.
        /// </summary>
        /// <param name="element">The element.</param>
        /// <returns>The list of binding expressions.</returns>
        public static IList<BindingExpression> GetBindingExpressionsForElement(FrameworkElement element)
        {
            List<BindingExpression> bindingExpressions = new List<BindingExpression>();

            if (element == null)
            {
                return bindingExpressions;
            }

            List<DependencyProperty> dependencyProperties = GetDependencyPropertiesForElement(element);
            Debug.Assert(dependencyProperties != null, "GetDependencyPropertiesForElement() should never return null.");

            foreach (DependencyProperty dependencyProperty in dependencyProperties)
            {
                if (dependencyProperty != null)
                {
                    BindingExpression bindingExpression = element.GetBindingExpression(dependencyProperty);

                    if (bindingExpression != null &&
                        bindingExpression.ParentBinding != null &&
                        bindingExpression.ParentBinding.Path != null &&
                        !string.IsNullOrEmpty(bindingExpression.ParentBinding.Path.Path) &&
                        bindingExpression.ParentBinding.Mode == BindingMode.TwoWay)
                    {
                        bindingExpressions.Add(bindingExpression);
                    }
                }
            }

            int childrenCount = VisualTreeHelper.GetChildrenCount(element);

            for (int i = 0; i < childrenCount; i++)
            {
                FrameworkElement childElement = VisualTreeHelper.GetChild(element, i) as FrameworkElement;

                // Stop if we've found a child DataForm or DataField.
                if (childElement != null && childElement.GetType() != typeof(DataForm) && childElement.GetType() != typeof(DataField))
                {
                    bindingExpressions.AddRange(GetBindingExpressionsForElement(childElement));
                }
            }

            return bindingExpressions;
        }
开发者ID:modulexcite,项目名称:SilverlightToolkit,代码行数:49,代码来源:ValidationUtil.cs


注:本文中的FrameworkElement.GetBindingExpression方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。