From 963115f6db74a5e0371066f856ed5150a68c261d Mon Sep 17 00:00:00 2001 From: Stephane Delcroix Date: Thu, 10 Oct 2024 12:06:35 +0200 Subject: [PATCH] [X] inherit DataType based on attribute Change the heuristic for deciding if we need ot ignore parent DataType, as DataTrigger.Binding shoud inherit it, and Picker.ItemDisplayNameBinding should not this is an alternate fix for #23989, partially replaces #24513, and will help fixing #25141 together with #24152 --- src/Controls/src/Core/ListView/ListView.cs | 3 +++ src/Controls/src/Core/Picker/Picker.cs | 2 ++ src/Controls/src/Core/TemplatedItemsList.cs | 3 +++ .../src/Core/Xaml/DoesNotInheritDataTypeAttribute.cs | 9 +++++++++ src/Controls/src/Xaml/XamlServiceProvider.cs | 8 ++++---- 5 files changed, 21 insertions(+), 4 deletions(-) create mode 100644 src/Controls/src/Core/Xaml/DoesNotInheritDataTypeAttribute.cs diff --git a/src/Controls/src/Core/ListView/ListView.cs b/src/Controls/src/Core/ListView/ListView.cs index ac067b2712d6..73e14502dd54 100644 --- a/src/Controls/src/Core/ListView/ListView.cs +++ b/src/Controls/src/Core/ListView/ListView.cs @@ -8,6 +8,7 @@ using System.Windows.Input; using Microsoft.Extensions.Logging; using Microsoft.Maui.Controls.Internals; +using Microsoft.Maui.Controls.Xaml; using Microsoft.Maui.Controls.Xaml.Diagnostics; using Microsoft.Maui.Devices; using Microsoft.Maui.Graphics; @@ -151,6 +152,7 @@ protected override void OnBindingContextChanged() } /// + [DoesNotInheritDataType] public BindingBase GroupDisplayBinding { get { return _groupDisplayBinding; } @@ -176,6 +178,7 @@ public DataTemplate GroupHeaderTemplate } /// + [DoesNotInheritDataType] public BindingBase GroupShortNameBinding { get { return _groupShortNameBinding; } diff --git a/src/Controls/src/Core/Picker/Picker.cs b/src/Controls/src/Core/Picker/Picker.cs index 85f282d717c5..cfc071ad248e 100644 --- a/src/Controls/src/Core/Picker/Picker.cs +++ b/src/Controls/src/Core/Picker/Picker.cs @@ -7,6 +7,7 @@ using System.ComponentModel; using System.Runtime.CompilerServices; using Microsoft.Maui.Controls.Internals; +using Microsoft.Maui.Controls.Xaml; using Microsoft.Maui.Graphics; namespace Microsoft.Maui.Controls @@ -199,6 +200,7 @@ public TextAlignment VerticalTextAlignment BindingBase _itemDisplayBinding; /// + [DoesNotInheritDataType] public BindingBase ItemDisplayBinding { get { return _itemDisplayBinding; } diff --git a/src/Controls/src/Core/TemplatedItemsList.cs b/src/Controls/src/Core/TemplatedItemsList.cs index a90d29d71212..d4c2bd035313 100644 --- a/src/Controls/src/Core/TemplatedItemsList.cs +++ b/src/Controls/src/Core/TemplatedItemsList.cs @@ -9,6 +9,7 @@ using System.Threading.Tasks; using Cadenza.Collections; using Microsoft.Maui.Controls.Internals; +using Microsoft.Maui.Controls.Xaml; using Microsoft.Maui.Devices; namespace Microsoft.Maui.Controls.Internals @@ -100,6 +101,7 @@ event PropertyChangedEventHandler ITemplatedItemsList.PropertyChanged remove { PropertyChanged -= value; } } + [DoesNotInheritDataType] public BindingBase GroupDisplayBinding { get { return _groupDisplayBinding; } @@ -133,6 +135,7 @@ public DataTemplate GroupHeaderTemplate public BindableProperty GroupHeaderTemplateProperty { get; set; } + [DoesNotInheritDataType] public BindingBase GroupShortNameBinding { get { return _groupShortNameBinding; } diff --git a/src/Controls/src/Core/Xaml/DoesNotInheritDataTypeAttribute.cs b/src/Controls/src/Core/Xaml/DoesNotInheritDataTypeAttribute.cs new file mode 100644 index 000000000000..6c1f785c6f2d --- /dev/null +++ b/src/Controls/src/Core/Xaml/DoesNotInheritDataTypeAttribute.cs @@ -0,0 +1,9 @@ +#nullable disable +using System; + +namespace Microsoft.Maui.Controls.Xaml +{ + internal class DoesNotInheritDataTypeAttribute : Attribute + { + } +} diff --git a/src/Controls/src/Xaml/XamlServiceProvider.cs b/src/Controls/src/Xaml/XamlServiceProvider.cs index c2b86f47db39..5b2e3bbbd9e2 100644 --- a/src/Controls/src/Xaml/XamlServiceProvider.cs +++ b/src/Controls/src/Xaml/XamlServiceProvider.cs @@ -324,7 +324,7 @@ static bool IsBindingContextBinding(IElementNode node) return false; } - static bool IsBindingBaseProperty(IElementNode node, HydrationContext context) + static bool DoesNotInheritDataType(IElementNode node, HydrationContext context) { if ( node.TryGetPropertyName(node.Parent, out XmlName name) && node.Parent is IElementNode parent @@ -332,9 +332,9 @@ static bool IsBindingBaseProperty(IElementNode node, HydrationContext context) new XmlLineInfo(((IXmlLineInfo)node).LineNumber, ((IXmlLineInfo)node).LinePosition), context.RootElement.GetType().Assembly, out var xpe) is Type parentType && parentType.GetRuntimeProperties().FirstOrDefault(p => p.Name == name.LocalName) is PropertyInfo propertyInfo - && propertyInfo.PropertyType == typeof(BindingBase)) + && propertyInfo.CustomAttributes.Any(ca => ca.AttributeType == typeof(DoesNotInheritDataTypeAttribute))) { - return true; + return true; } return false; } @@ -360,7 +360,7 @@ static bool IsBindingBaseProperty(IElementNode node, HydrationContext context) { break; } - if (IsBindingBaseProperty(n, context)) + if (DoesNotInheritDataType(n, context)) { break; }