Skip to content

Commit

Permalink
fix(Binding): x:Bind to fields not applied
Browse files Browse the repository at this point in the history
- in some cases, x:Bind to fields is not applied, because the ValueType is returned null.
  • Loading branch information
MartinZikmund committed Jan 12, 2021
1 parent e4e76b6 commit 875a722
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/Uno.UI/DataBinding/BindingPath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ public Type PropertyType
{
if (DataContext != null)
{
return BindingPropertyHelper.GetPropertyType(_dataContextType, PropertyName);
return BindingPropertyHelper.GetPropertyType(_dataContextType, PropertyName, _allowPrivateMembers);
}
else
{
Expand Down
20 changes: 15 additions & 5 deletions src/Uno.UI/DataBinding/BindingPropertyHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ internal static partial class BindingPropertyHelper
private static Dictionary<CachedTuple<Type, string, DependencyPropertyValuePrecedences>, ValueGetterHandler> _getSubstituteValueGetter = new Dictionary<CachedTuple<Type, string, DependencyPropertyValuePrecedences>, ValueGetterHandler>(CachedTuple<Type, string, DependencyPropertyValuePrecedences>.Comparer);
private static Dictionary<CachedTuple<Type, string, DependencyPropertyValuePrecedences>, ValueUnsetterHandler> _getValueUnsetter = new Dictionary<CachedTuple<Type, string, DependencyPropertyValuePrecedences>, ValueUnsetterHandler>(CachedTuple<Type, string, DependencyPropertyValuePrecedences>.Comparer);
private static Dictionary<CachedTuple<Type, string>, bool> _isEvent = new Dictionary<CachedTuple<Type, string>, bool>(CachedTuple<Type, string>.Comparer);
private static Dictionary<CachedTuple<Type, string>, Type?> _getPropertyType = new Dictionary<CachedTuple<Type, string>, Type?>(CachedTuple<Type, string>.Comparer);
private static Dictionary<CachedTuple<Type, string, bool>, Type?> _getPropertyType = new Dictionary<CachedTuple<Type, string, bool>, Type?>(CachedTuple<Type, string, bool>.Comparer);

static BindingPropertyHelper()
{
Expand Down Expand Up @@ -96,17 +96,17 @@ public static bool IsEvent(Type type, string property)
return result;
}

public static Type? GetPropertyType(Type type, string property)
public static Type? GetPropertyType(Type type, string property, bool allowPrivateMembers)
{
var key = CachedTuple.Create(type, property);
var key = CachedTuple.Create(type, property, allowPrivateMembers);

Type? result;

lock (_getPropertyType)
{
if (!_getPropertyType.TryGetValue(key, out result))
{
_getPropertyType.Add(key, result = InternalGetPropertyType(type, property));
_getPropertyType.Add(key, result = InternalGetPropertyType(type, property, allowPrivateMembers));
}
}

Expand Down Expand Up @@ -227,7 +227,7 @@ private static bool InternalIsEvent(Type type, string property)
#endif
}

private static Type? InternalGetPropertyType(Type type, string property)
private static Type? InternalGetPropertyType(Type type, string property, bool allowPrivateMembers)
{
if(type == typeof(UnsetValue))
{
Expand Down Expand Up @@ -303,6 +303,16 @@ private static bool InternalIsEvent(Type type, string property)
return propertyInfo.PropertyType;
}

// Look for a field (permitted for x:Bind only)
if (allowPrivateMembers)
{
var fieldInfo = GetFieldInfo(type, property, true);
if (fieldInfo != null)
{
return fieldInfo.FieldType;
}
}

// Look for an attached property
var attachedPropertyGetter = GetAttachedPropertyGetter(type, property);

Expand Down
6 changes: 3 additions & 3 deletions src/Uno.UI/UI/Xaml/DependencyObjectStore.Binder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ public void SetBinding(string dependencyProperty, BindingBase binding)
if (fullBinding != null)
{
var boundProperty = DependencyProperty.GetProperty(_originalObjectType, dependencyProperty)
?? FindStandardProperty(_originalObjectType, dependencyProperty);
?? FindStandardProperty(_originalObjectType, dependencyProperty, fullBinding.CompiledSource != null);

if (boundProperty != null)
{
Expand All @@ -390,9 +390,9 @@ public void SetBinding(string dependencyProperty, BindingBase binding)
/// <summary>
/// Finds a DependencyProperty for the specified C# property
/// </summary>
private DependencyProperty? FindStandardProperty(Type originalObjectType, string dependencyProperty)
private DependencyProperty? FindStandardProperty(Type originalObjectType, string dependencyProperty, bool allowPrivateMembers)
{
var propertyType = BindingPropertyHelper.GetPropertyType(originalObjectType, dependencyProperty);
var propertyType = BindingPropertyHelper.GetPropertyType(originalObjectType, dependencyProperty, allowPrivateMembers);

if (propertyType != null)
{
Expand Down

0 comments on commit 875a722

Please sign in to comment.