Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions src/Controls/src/Core/BindingExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,28 @@ PropertyInfo GetIndexer(TypeInfo sourceType, string indexerName, string content)
return null;
}

PropertyInfo GetProperty(TypeInfo sourceType, string propertyName)
{
// First, check the type and its base classes
TypeInfo type = sourceType;
do
{
var property = type.GetDeclaredProperty(propertyName);
if (property != null)
return property;
} while ((type = type.BaseType?.GetTypeInfo()) != null);

// If not found, check implemented interfaces (for interface-inherited properties like IReadOnlyList<T>.Count)
foreach (var iface in sourceType.ImplementedInterfaces)
{
var property = GetProperty(iface.GetTypeInfo(), propertyName);
if (property != null)
return property;
}

return null;
}


void SetupPart(TypeInfo sourceType, BindingExpressionPart part)
{
Expand Down Expand Up @@ -382,11 +404,7 @@ void SetupPart(TypeInfo sourceType, BindingExpressionPart part)
}
else
{
TypeInfo type = sourceType;
do
{
property = type.GetDeclaredProperty(part.Content);
} while (property == null && (type = type.BaseType?.GetTypeInfo()) != null);
property = GetProperty(sourceType, part.Content);
}
if (property != null)
{
Expand Down
1 change: 1 addition & 0 deletions src/Controls/tests/SourceGen.UnitTests/Maui32879Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public TestPage()
// </auto-generated>
//------------------------------------------------------------------------------
#nullable enable
#pragma warning disable CS0219 // Variable is assigned but its value is never used

namespace Test;

Expand Down
12 changes: 12 additions & 0 deletions src/Controls/tests/Xaml.UnitTests/Issues/Maui13872.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:Microsoft.Maui.Controls.Xaml.UnitTests"
x:Class="Microsoft.Maui.Controls.Xaml.UnitTests.Maui13872">
<VerticalStackLayout>
<Label x:Name="label0" Text="{Binding List.Count}"/>
<Label x:Name="label1" Text="{Binding ListCount}"/>
<Label x:Name="label2" Text="{Binding List.Count}" x:DataType="local:Maui13872ViewModel"/>
<Label x:Name="label3" Text="{Binding ListCount}" x:DataType="local:Maui13872ViewModel"/>
</VerticalStackLayout>
</ContentPage>
38 changes: 38 additions & 0 deletions src/Controls/tests/Xaml.UnitTests/Issues/Maui13872.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System.Collections.Generic;
using NUnit.Framework;

namespace Microsoft.Maui.Controls.Xaml.UnitTests;

public partial class Maui13872 : ContentPage
{
public Maui13872() => InitializeComponent();

[TestFixture]
class Tests
{
[Test]
public void CompiledBindingToIReadOnlyListCount([Values] XamlInflator inflator)
{
var page = new Maui13872(inflator);
page.BindingContext = new Maui13872ViewModel();

// Uncompiled bindings (no x:DataType) - should work with all inflators
Assert.That(page.label0.Text, Is.EqualTo("3"), "Uncompiled binding to List.Count");
Assert.That(page.label1.Text, Is.EqualTo("3"), "Uncompiled binding to ListCount");

// Compiled bindings (with x:DataType) - IReadOnlyList<T>.Count should resolve correctly.
// Count is defined on IReadOnlyCollection<T> which IReadOnlyList<T> inherits.
Assert.That(page.label2.Text, Is.EqualTo("3"), "Compiled binding to List.Count");
Assert.That(page.label3.Text, Is.EqualTo("3"), "Compiled binding to ListCount");
}
}
}

public class Maui13872ViewModel
{
private readonly string[] _list = ["Bill", "Steve", "John"];

public IReadOnlyList<string> List => _list;

public int ListCount => _list.Length;
}
Loading