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


C# FrameworkElement.GetType方法代码示例

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


在下文中一共展示了FrameworkElement.GetType方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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: ValidateTemplatedParent

        /// <summary>
        ///     Validate against the following rules
        ///     1. One cannot use a ControlTemplate to template a FrameworkContentElement
        ///     2. One cannot use a ControlTemplate to template a FrameworkElement other than a Control 
        ///     3. One cannot use a ControlTemplate to template a Control that isn't associated with it
        /// </summary> 
        protected override void ValidateTemplatedParent(FrameworkElement templatedParent) 
        {
            // Must have a non-null feTemplatedParent 
            if (templatedParent == null)
            {
                throw new ArgumentNullException("templatedParent");
            } 

            // The target type of a ControlTemplate must match the 
            // type of the Control that it is being applied to 
            if (_targetType != null && !_targetType.IsInstanceOfType(templatedParent))
            { 
                throw new ArgumentException(SR.Get(SRID.TemplateTargetTypeMismatch, _targetType.Name, templatedParent.GetType().Name));
            }

            // One cannot use a ControlTemplate to template a Control that isn't associated with it 
            if (templatedParent.TemplateInternal != this)
            { 
                throw new ArgumentException(SR.Get(SRID.MustNotTemplateUnassociatedControl)); 
            }
        } 
开发者ID:sjyanxin,项目名称:WPFSource,代码行数:27,代码来源:ControlTemplate.cs

示例3: ValidateTemplatedParent

        //-------------------------------------------------------------------
        //
        //  Protected Methods
        // 
        //-------------------------------------------------------------------
 
        /// <summary> 
        ///     Validate against the following rules
        ///     1. Must have a non-null feTemplatedParent 
        ///     2. A ItemsPanelTemplate must be applied to a ContentPresenter
        /// </summary>
        protected override void ValidateTemplatedParent(FrameworkElement templatedParent)
        { 
            // Must have a non-null feTemplatedParent
            if (templatedParent == null) 
            { 
                throw new ArgumentNullException("templatedParent");
            } 

            // A ItemsPanelTemplate must be applied to an ItemsPresenter
            if (!(templatedParent is ItemsPresenter))
            { 
                throw new ArgumentException(SR.Get(SRID.TemplateTargetTypeMismatch, "ItemsPresenter", templatedParent.GetType().Name));
            } 
        } 
开发者ID:sjyanxin,项目名称:WPFSource,代码行数:25,代码来源:ItemsPanelTemplate.cs

示例4: FindParent

        internal static DependencyObject FindParent(FrameworkElement target)
        {
            IBindingMemberInfo member = BindingServiceProvider
                .MemberProvider
                .GetBindingMember(target.GetType(), "PlacementTarget", false, false);
            if (member != null)
            {
                object value = member.GetValue(target, null);
                if (value != null)
                    return (DependencyObject)value;
            }
#if WPF
            if (target.IsLoaded)
                return target.Parent ?? VisualTreeHelper.GetParent(target) ?? LogicalTreeHelper.GetParent(target);
            return target.Parent;
#else
            return target.Parent ?? VisualTreeHelper.GetParent(target);
#endif
        }
开发者ID:FilipHerman,项目名称:MugenMvvmToolkit,代码行数:19,代码来源:ParentObserver.cs

示例5: ResolveBeginStoryboardName

    /// <summary>
    ///     Finds a BeginStoryboard with the given name, following the rules
    /// governing Storyboard.  Returns null if not found.
    /// </summary>
    /// <remarks>
    ///     If a name scope is given, look there and nowhere else.  In the
    /// absense of name scope, use Framework(Content)Element.FindName which
    /// has its own complex set of rules for looking up name scopes.
    ///
    ///     This is a different set of rules than from that used to look up
    /// the TargetName.  BeginStoryboard name is registered with the template
    /// INameScope on a per-template basis.  So we look it up using
    /// INameScope.FindName().  This is a function completely different from
    /// Template.FindName().
    /// </remarks>
    internal static BeginStoryboard ResolveBeginStoryboardName(
        string targetName,
        INameScope nameScope,
        FrameworkElement fe,
        FrameworkContentElement fce)
    {
        object          namedObject = null;
        BeginStoryboard beginStoryboard = null;

        if( nameScope != null )
        {
            namedObject = nameScope.FindName(targetName);
            if( namedObject == null )
            {
                throw new InvalidOperationException(
                    SR.Get(SRID.Storyboard_NameNotFound, targetName, nameScope.GetType().ToString()));
            }
        }
        else if( fe != null )
        {
            namedObject = fe.FindName(targetName);
            if( namedObject == null )
            {
                throw new InvalidOperationException(
                    SR.Get(SRID.Storyboard_NameNotFound, targetName, fe.GetType().ToString()));
            }
        }
        else if( fce != null )
        {
            namedObject = fce.FindName(targetName);
            if( namedObject == null )
            {
                throw new InvalidOperationException(
                    SR.Get(SRID.Storyboard_NameNotFound, targetName, fce.GetType().ToString()));
            }
        }
        else
        {
            throw new InvalidOperationException(
                SR.Get(SRID.Storyboard_NoNameScope, targetName));
        }

        beginStoryboard = namedObject as BeginStoryboard;

        if( beginStoryboard == null )
        {
            throw new InvalidOperationException(SR.Get(SRID.Storyboard_BeginStoryboardNameNotFound, targetName));
        }

        return beginStoryboard;
    }
开发者ID:nlh774,项目名称:DotNetReferenceSource,代码行数:66,代码来源:Storyboard.cs

示例6: ApplyBindings

 protected override void ApplyBindings(FrameworkElement attachedObject,
                                       IEnumerable<MvxBindingDescription> bindingDescriptions)
 {
     var actualType = attachedObject.GetType();
     foreach (var bindingDescription in bindingDescriptions)
     {
         ApplyBinding(bindingDescription, actualType, attachedObject);
     }
 }
开发者ID:talisqualis,项目名称:MvvmCross-Build,代码行数:9,代码来源:MvxWindowsBindingCreator.cs

示例7: HookEvent

    private void HookEvent(FrameworkElement oldTarget, string oldEvent, FrameworkElement newTarget, string newEvent)
    {
      if (!ReferenceEquals(oldTarget, newTarget) || oldEvent != newEvent)
      {
        if (oldTarget != null && !string.IsNullOrEmpty(oldEvent))
        {
          var eventRef = oldTarget.GetType().GetEvent(oldEvent);
          if (eventRef != null)
          {
            var invoke = eventRef.EventHandlerType.GetMethod("Invoke");
            var p = invoke.GetParameters();
            if (p.Length == 2)
            {
              var p1Type = p[1].ParameterType;
              if (typeof(EventArgs).IsAssignableFrom(p1Type))
              {
                var handler = this.GetType().GetMethod("CallMethod", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic);
#if WINDOWS_UWP
                var del = handler.CreateDelegate(eventRef.EventHandlerType, this);
#else
                var del = Delegate.CreateDelegate(eventRef.EventHandlerType, this, handler);
#endif
                eventRef.RemoveEventHandler(oldTarget, del);
              }
              else
              {
                throw new NotSupportedException("Invalid trigger event");
              }
            }
            else
              throw new NotSupportedException("Invalid trigger event");
          }
        }

        if (newTarget != null && !string.IsNullOrEmpty(newEvent))
        {
          var eventRef = newTarget.GetType().GetEvent(newEvent);
          if (eventRef != null)
          {
            var invoke = eventRef.EventHandlerType.GetMethod("Invoke");
            var p = invoke.GetParameters();
            if (p.Length == 2)
            {
              var p1Type = p[1].ParameterType;
              if (typeof(EventArgs).IsAssignableFrom(p1Type))
              {
                var handler = this.GetType().GetMethod("CallMethod", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic);
#if WINDOWS_UWP
                var del = handler.CreateDelegate(eventRef.EventHandlerType, this);
#else
                var del = Delegate.CreateDelegate(eventRef.EventHandlerType, this, handler);
#endif
                eventRef.AddEventHandler(newTarget, del);
              }
              else
              {
                throw new NotSupportedException("Invalid trigger event");
              }
            }
            else
              throw new NotSupportedException("Invalid trigger event");
          }
        }
      }
    }
开发者ID:transformersprimeabcxyz,项目名称:Bxf-MarimerLLC,代码行数:65,代码来源:TriggerAction.cs

示例8: FindParent

 internal static DependencyObject FindParent(FrameworkElement target)
 {
     IBindingMemberInfo member = BindingServiceProvider
         .MemberProvider
         .GetBindingMember(target.GetType(), "PlacementTarget", false, false);
     if (member != null)
     {
         object value = member.GetValue(target, null);
         if (value == null)
             return null;
         return (DependencyObject)value;
     }
     return VisualTreeHelper.GetParent(target) ?? target.Parent;
 }
开发者ID:windygu,项目名称:MugenMvvmToolkit,代码行数:14,代码来源:ParentObserver.cs

示例9: OnTargetPropertyUpdated

        /// <summary>
        /// is called after a target-property is changed
        /// </summary>
        /// <param name="element"></param>
        /// <param name="dp"></param>
        internal void OnTargetPropertyUpdated (FrameworkElement element, DependencyProperty dp) {
			var prop = element.GetType ().GetProperty (dp.Name);
			var value = prop.GetValue (element, null);

			this.SetSourceValue (value);
            OnTargetUpdated();
        }
开发者ID:bbqchickenrobot,项目名称:WPFLight,代码行数:12,代码来源:Binding.cs

示例10: GetDependencyPropertiesForElement

        /// <summary>
        /// Gets the list of dependency properties for the given element.
        /// </summary>
        /// <param name="element">The element.</param>
        /// <returns>The list of dependency properties.</returns>
        public static List<DependencyProperty> GetDependencyPropertiesForElement(FrameworkElement element)
        {
            List<DependencyProperty> dependencyProperties = new List<DependencyProperty>();

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

            bool isBlocklisted =
                element is Panel || element is Button || element is Image || element is ScrollViewer || element is TextBlock ||
                element is Border || element is Shape || element is ContentPresenter || element is RangeBase;

            if (!isBlocklisted)
            {
                FieldInfo[] fields = element.GetType().GetFields(BindingFlags.Static | BindingFlags.Public | BindingFlags.FlattenHierarchy);

                if (fields != null)
                {
                    foreach (FieldInfo field in fields)
                    {
                        if (field.FieldType == typeof(DependencyProperty))
                        {
                            dependencyProperties.Add((DependencyProperty)field.GetValue(null));
                        }
                    }
                }
            }

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

示例11: ApplyBinding

            /// <summary>
            /// Applies the Binding represented by the SetterValueBindingHelper.
            /// </summary>
            /// <param name="element">Element to apply the Binding to.</param>
            /// <param name="item">SetterValueBindingHelper representing the Binding.</param>
            private static void ApplyBinding(FrameworkElement element, SetterValueBindingHelper item)
            {
                if ((null == item.Property) || (null == item.Binding))
                {
                    throw new ArgumentException(
                        "SetterValueBindingHelper's Property and Binding must both be set to non-null values.");
                }

                // Get the type on which to set the Binding
                TypeInfo typeInfo = null;
                if (null == item.Type)
                {
                    // No type specified; setting for the specified element
                    typeInfo = element.GetType().GetTypeInfo();
                }
                else
                {
                    // Try to get the type from the type system
                    var type = System.Type.GetType(item.Type);
                    if (type != null)
                    {
                        typeInfo = type.GetTypeInfo();
                    }

                    if (type == null)
                    {

                        // Search for the type in the list of assemblies
                        foreach (var assembly in AssembliesToSearch)
                        {
                            // Match on short or full name
                            typeInfo = assembly.DefinedTypes.FirstOrDefault(t => (t.FullName == item.Type) || (t.Name == item.Type));
                            if (null != typeInfo)
                            {
                                // Found; done searching
                                break;
                            }
                        }
                        if (null == typeInfo)
                        {
                            // Unable to find the requested type anywhere
                            throw new ArgumentException(
                                string.Format(
                                    CultureInfo.CurrentCulture,
                                    "Unable to access type \"{0}\". Try using an assembly qualified type name.",
                                    item.Type));
                        }
                    }
                }

                // Get the DependencyProperty for which to set the Binding
                DependencyProperty property = null;

                var field = GetDPField(item.Property, typeInfo);

                if (null != field)
                {
                    property = field.GetValue(null) as DependencyProperty;
                }
                if (null == property)
                {
                    // Unable to find the requsted property
                    throw new ArgumentException(
                        string.Format(
                            CultureInfo.CurrentCulture,
                            "Unable to access DependencyProperty \"{0}\" on type \"{1}\".",
                            item.Property,
                            typeInfo.Name));
                }

                // Set the specified Binding on the specified property
                element.SetBinding(property, item.Binding);
            }
开发者ID:modulexcite,项目名称:WizardControl-UWP,代码行数:78,代码来源:SetterValueBindingHelper.cs

示例12: SetFocus

        public static void SetFocus(FrameworkElement element)
        {
            if (element is UserControl)
            {
                ((UserControl) element).Focus();
                //RaiseFocuChanged(new FocusChangedArgs(element, Root));
                return;
            }

            throw new NotSupportedException("Cannot set focus on object of type {0}".With(element.GetType()));
        }
开发者ID:bobasoft,项目名称:framework,代码行数:11,代码来源:FocusManager.cs


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