private static IPropertyObserver CreateBaseObserver(Type containingType, XamlName propertyName) { if (propertyName.IsEmpty) { return(null); } containingType = propertyName.ResolveContainingType(containingType); DependencyProperty dependencyProperty = DependencyProperty.GetProperty(containingType, propertyName.MemberName); if (dependencyProperty != null) { return(new DependencyPropertyObserver(dependencyProperty)); } PropertyInfo propertyInfo = containingType.GetInstanceProperty(propertyName.MemberName); if (propertyInfo != null && !propertyInfo.GetIndexParameters().Any()) { return(new ClrPropertyObserver(propertyInfo, new object[0])); } return(null); }
private static bool TryCreateAdapter(TypeMemberKey key, out IPropertyAdapter adapter) { adapter = null; if (key.MemberName.IsEmpty) { return(false); } DependencyProperty dependencyProperty = DependencyProperty.GetProperty(key.Type, key.MemberName); if (dependencyProperty != null) { adapter = new DependencyPropertyAdapter(dependencyProperty); return(true); } PropertyInfo clrProperty = GetClrProperty(key.Type, key.MemberName); if (clrProperty != null) { adapter = new ClrPropertyAdapter(clrProperty); return(true); } return(false); }
public static DependencyProperty GetProperty(string ownerTypeName, string propertyName) { var ownerType = Type.GetType(ownerTypeName + ",Uno.UI.Toolkit"); var dp = DependencyProperty.GetProperty(ownerType, propertyName); return(dp); }
internal static void SetValueBypassPropagation(this Timeline timeline, object value) { if (timeline.Log().IsEnabled(Uno.Foundation.Logging.LogLevel.Debug)) { timeline.Log().DebugFormat("Setting [{0}] to [{1} / {2}] and bypassing native propagation", value, Storyboard.GetTargetName(timeline), Storyboard.GetTargetProperty(timeline)); } var animatedItem = timeline.PropertyInfo.GetPathItems().Last(); // Get the property name from the last part of the // specified name (for dotted paths, if they exist) var propertyName = animatedItem.PropertyName.Split(new[] { '.' }).Last().Replace("(", "").Replace(")", ""); var dc = animatedItem.DataContext; using (dc != null ? DependencyObjectStore.BypassPropagation( (DependencyObject)dc, DependencyProperty.GetProperty(dc.GetType(), propertyName) ) : // DC may have been collected since it's weakly held null ) { timeline.PropertyInfo.Value = value; } }
public void DependencyPropertyAmbiguityTest() { ElementB element = new ElementB(); element.SetValue(ElementA.Value0Property, 1); element.SetValue(ElementB.Value0Property2, 2); Assert.AreEqual(1, element.GetValue(ElementA.Value0Property)); Assert.AreEqual(2, element.GetValue(ElementB.Value0Property2)); Assert.AreEqual(ElementA.Value0Property, DependencyProperty.GetProperty(typeof(ElementA), "Value0")); Assert.AreEqual(ElementB.Value0Property2, DependencyProperty.GetProperty(typeof(ElementB), "Value0")); }
internal static void SetValueBypassPropagation(this Timeline timeline, object value) { if (timeline.Log().IsEnabled(Microsoft.Extensions.Logging.LogLevel.Debug)) { timeline.Log().DebugFormat("Setting [{0}] to [{1} / {2}] and bypassing native propagation", value, Storyboard.GetTargetName(timeline), Storyboard.GetTargetProperty(timeline)); } var animatedItem = timeline.PropertyInfo.GetPathItems().Last(); using ( DependencyObjectStore.BypassPropagation( (DependencyObject)animatedItem.DataContext, DependencyProperty.GetProperty(animatedItem.DataContext.GetType(), animatedItem.PropertyName) ) ) { timeline.PropertyInfo.Value = value; } }
public override void OnAttach(object obj, string propertyName) { var d = obj as DependencyObject; if (d == null) { throw new XamlException("模板绑定表达式应用的对象必须是" + typeof(DependencyObject).FullName); } var dp = DependencyProperty.GetProperty(d.ObjectType, propertyName); if (dp != null) { OnAttach(d, dp); } else { _target = d; } //如果没有对应的dp,那么不需要通知触发属性变化事件 }
public static IPropertyAdapter CreateAdapter(Type targetType, XamlName propertyName) { if (propertyName.IsEmpty) { return(null); } DependencyProperty dependencyProperty = DependencyProperty.GetProperty(targetType, propertyName); if (dependencyProperty != null) { return(new DependencyPropertyAdapter(dependencyProperty)); } PropertyInfo clrProperty = GetClrProperty(targetType, propertyName); if (clrProperty != null) { return(new ClrPropertyAdapter(clrProperty)); } return(null); }
private void SynchronizeVerticalContentAlignment(VerticalAlignment?alignment = null) { var childControl = ContentTemplateRoot as FrameworkElement; if (childControl != null) { childControl.SetValue( FrameworkElement.VerticalAlignmentProperty, alignment ?? VerticalContentAlignment, DependencyPropertyValuePrecedences.Inheritance ); } else { var childControl2 = ContentTemplateRoot as IFrameworkElement; if (childControl2 != null) { // This case is for controls that implement IFrameworkElement, but do not inherit from FrameworkElement (like TextBlock). var verticalAlignmentProperty = DependencyProperty.GetProperty(ContentTemplateRoot.GetType(), "VerticalAlignment"); if (verticalAlignmentProperty != null) { childControl2.SetValue( verticalAlignmentProperty, alignment ?? VerticalContentAlignment, DependencyPropertyValuePrecedences.Inheritance ); } else { throw new InvalidOperationException($"The property VerticalAlignment should exist on type {ContentTemplateRoot.GetType()}"); } } } }
/// <summary> /// 加载单个简单属性 /// </summary> /// <param name="objType"></param> /// <param name="obj"></param> /// <param name="propertyName"></param> /// <param name="attr"></param> private static void LoadSingleSimple(Type objType, object obj, string propertyName, string attrValue) { if (propertyName.Equals("xmlns", StringComparison.OrdinalIgnoreCase) || propertyName.StartsWith("xmlns:", StringComparison.OrdinalIgnoreCase) || propertyName.Equals("x:key", StringComparison.OrdinalIgnoreCase)) { return; } if (propertyName.Equals("x:Name", StringComparison.OrdinalIgnoreCase)) { propertyName = "name"; } var propertyInfo = objType.ResolveProperty(propertyName); if (propertyInfo == null) { var element = obj as IHtmlElementCore; if (element == null) { throw new XamlException("类型" + objType.FullName + "没有实现" + typeof(IHtmlElementCore).FullName + ",不能使用自定义属性"); } var e = CreateMarkupExtension(attrValue); if (e != null) { var value = e.ProvideValue(obj, propertyName); element.Attributes.SetValue(obj, propertyName, value); } else { element.Attributes.SetValue(obj, propertyName, attrValue); } } else { var d = obj as DependencyObject; var e = CreateMarkupExtension(attrValue); if (e != null) { if (d == null) { throw new XamlException("类型" + objType.FullName + "不是依赖对象,不能应用标记扩展"); } //标记扩展 var dp = DependencyProperty.GetProperty(objType, propertyInfo.Name); if (dp == null) { throw new XamlException("类型" + objType.FullName + "的属性" + propertyName + "不是依赖属性,不能应用标记扩展"); } var value = e.ProvideValue(obj, dp); d.SetValue(dp, value); } else { //根据字符串的值,转换成具体类型赋值 var value = GetValue(attrValue, propertyInfo); propertyInfo.SetValue(obj, value); } } }
public void Property_ByName() { DependencyProperty.GetProperty(typeof(Border), "Child"); }
public override void OnAttach(DependencyObject d, DependencyProperty dp) { _target = d; _targetProperty = dp; var templateCell = _target as ITemplateCell; if (templateCell == null) { throw new XamlException("模板绑定表达式应用的对象必须是" + typeof(ITemplateCell).FullName); } var template = templateCell.BelongTemplate; //得到应用绑定表达式对象所在的模板对象 if (template == null) { throw new XamlException("无法获取对象所在模板,不能应用模板绑定表达式" + _target.GetType().FullName); } //截获模板的应用对象被改变事件 template.TemplateParentChanged += (s, e) => { if (_source != null) { //移除老对象上绑定的事件 var dependencySource = _source as DependencyObject; if (dependencySource != null) { IList <DependencyPropertyChangedEventHandler> handlers = null; if (_events.TryGetValue(dependencySource, out handlers)) { foreach (var handler in handlers) { dependencySource.RemovePropertyChanged(_sourceProperty, handler); } _events.Remove(dependencySource); } } } { //在新的应用对象上追加事件 _source = LocateSource(e.NewValue); var dependencySource = _source as DependencyObject; if (_source == null) { return; } _sourceProperty = DependencyProperty.GetProperty(dependencySource.ObjectType, this.PropertyName); if (_sourceProperty == null) { throw new XamlException("在类型" + dependencySource.GetType() + "和其继承链上没有找到依赖属性" + this.Path + "的定义"); } DependencyPropertyChangedEventHandler handler = (sp, ep) => { _target.OnPropertyChanged(_targetProperty, ep.NewValue, ep.OldValue); }; dependencySource.AddPropertyChanged(_sourceProperty, handler); _events.Add(dependencySource, handler); //由于改变了模板应用的对象,意味着_target的属性也被改变了,所以这里要主动触发一次 var newValue = dependencySource.GetValue(_sourceProperty); _target.OnPropertyChanged(_targetProperty, newValue, null); } }; }