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


C# DependencyProperty.Validate方法代码示例

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


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

示例1: SetValue

		public static void SetValue (INativeDependencyObjectWrapper wrapper, DependencyProperty dp, object value)
		{
			Type object_type;

			if (dp == null)
				throw new ArgumentNullException ("property");

			CheckNativeAndThread (wrapper);
			
			if (dp.DeclaringType != null && !dp.IsAttached) {
				if (!dp.DeclaringType.IsAssignableFrom (wrapper.GetType ()))
					throw new System.ArgumentException (string.Format ("The DependencyProperty '{2}', registered on type {0} can't be used to set a value on an object of type {1}", dp.DeclaringType.AssemblyQualifiedName, wrapper.GetType ().AssemblyQualifiedName, dp.Name));
			}
			
			if (value == null) {
				using (var val = new Value { k = NativeMethods.dependency_property_get_property_type(dp.Native), IsNull = true }) {
					var v = val;
					NativeMethods.dependency_object_set_value (wrapper.NativeHandle, dp.Native, ref v);
				}
				return;
			}

			if (value == DependencyProperty.UnsetValue) {
				// Setting to UnsetValue appears to be equivalent to ClearValue
				ClearValue (wrapper, dp);
				return;
			}

			// XXX we need this to work with all
			// INativeDependencyObjectWrapper, I expect..
			// but right now the only one other than DO is
			// Application, which doesn't have any
			// settable properties, so we're safe for now.
			if (wrapper is DependencyObject)
				dp.Validate ((DependencyObject)wrapper, dp, value);

			object_type = value.GetType ();

			bool type_valid = false;
			if (dp.PropertyType.IsGenericTypeDefinition) {
				Type t = object_type;
				while (t != null) {
					if (dp.PropertyType.IsAssignableFrom (t)) {
						type_valid = true;
						break;
					}

					if (t.IsGenericType && !t.IsGenericTypeDefinition)
						t = t.GetGenericTypeDefinition ();
					else
						t = t.BaseType;
				}
			}
			else {
				type_valid = dp.PropertyType.IsAssignableFrom (object_type);
			}

			if (!type_valid) {
				throw new ArgumentException (string.Format ("The DependencyProperty '{3}.{2}', whose property type is {0} can't be set to value whose type is {1}", dp.PropertyType.FullName, object_type.FullName, dp.Name, dp.DeclaringType.FullName));
			}
				                     
			using (var val = Value.FromObject (value, dp is CustomDependencyProperty)) {
				var v = val;
				NativeMethods.dependency_object_set_value (wrapper.NativeHandle, dp.Native, ref v);
			}
		}
开发者ID:fmaulanaa,项目名称:moon,代码行数:66,代码来源:NativeDependencyObjectHelper.cs


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