Skip to content

Commit

Permalink
[X] inherit DataType based on attribute
Browse files Browse the repository at this point in the history
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
  • Loading branch information
StephaneDelcroix authored and github-actions committed Oct 15, 2024
1 parent bae0a09 commit 963115f
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 4 deletions.
3 changes: 3 additions & 0 deletions src/Controls/src/Core/ListView/ListView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -151,6 +152,7 @@ protected override void OnBindingContextChanged()
}

/// <include file="../../docs/Microsoft.Maui.Controls/ListView.xml" path="//Member[@MemberName='GroupDisplayBinding']/Docs/*" />
[DoesNotInheritDataType]
public BindingBase GroupDisplayBinding
{
get { return _groupDisplayBinding; }
Expand All @@ -176,6 +178,7 @@ public DataTemplate GroupHeaderTemplate
}

/// <include file="../../docs/Microsoft.Maui.Controls/ListView.xml" path="//Member[@MemberName='GroupShortNameBinding']/Docs/*" />
[DoesNotInheritDataType]
public BindingBase GroupShortNameBinding
{
get { return _groupShortNameBinding; }
Expand Down
2 changes: 2 additions & 0 deletions src/Controls/src/Core/Picker/Picker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -199,6 +200,7 @@ public TextAlignment VerticalTextAlignment

BindingBase _itemDisplayBinding;
/// <include file="../../docs/Microsoft.Maui.Controls/Picker.xml" path="//Member[@MemberName='ItemDisplayBinding']/Docs/*" />
[DoesNotInheritDataType]
public BindingBase ItemDisplayBinding
{
get { return _itemDisplayBinding; }
Expand Down
3 changes: 3 additions & 0 deletions src/Controls/src/Core/TemplatedItemsList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -100,6 +101,7 @@ event PropertyChangedEventHandler ITemplatedItemsList<TItem>.PropertyChanged
remove { PropertyChanged -= value; }
}

[DoesNotInheritDataType]
public BindingBase GroupDisplayBinding
{
get { return _groupDisplayBinding; }
Expand Down Expand Up @@ -133,6 +135,7 @@ public DataTemplate GroupHeaderTemplate

public BindableProperty GroupHeaderTemplateProperty { get; set; }

[DoesNotInheritDataType]
public BindingBase GroupShortNameBinding
{
get { return _groupShortNameBinding; }
Expand Down
9 changes: 9 additions & 0 deletions src/Controls/src/Core/Xaml/DoesNotInheritDataTypeAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#nullable disable
using System;

namespace Microsoft.Maui.Controls.Xaml
{
internal class DoesNotInheritDataTypeAttribute : Attribute
{
}
}
8 changes: 4 additions & 4 deletions src/Controls/src/Xaml/XamlServiceProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -324,17 +324,17 @@ 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
&& XamlParser.GetElementType(parent.XmlType,
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;
}
Expand All @@ -360,7 +360,7 @@ static bool IsBindingBaseProperty(IElementNode node, HydrationContext context)
{
break;
}
if (IsBindingBaseProperty(n, context))
if (DoesNotInheritDataType(n, context))
{
break;
}
Expand Down

0 comments on commit 963115f

Please sign in to comment.