dotnet / dotnet/maui

[Bug] BindingContext default behavior, on nested controls

Open
#10,296 7 comments 0 reactions 0 assignees View on GitHub
area-xaml platform/android platform/ios platform/macos platform/windows s/triaged t/bug
Dominant language
C#
Stars
23.3k
Forks
2k
Avg merge
1d 15h
Merged PRs (30d)
290

Description

### Description

Hello,

I am trying to bind a Text Property of a control that is inside another, and it does not work.

Let me explain, I was creating kind of an Input layout, like so
````csharp
public partial class InputLayout : Grid
{
public static readonly BindableProperty LabelProperty =
BindableProperty.Create(nameof(Label), typeof(Label), typeof(InputLayout), null, propertyChanged: LabelChanged);

public static readonly BindableProperty InputViewProperty =
BindableProperty.Create(nameof(InputView), typeof(InputView), typeof(InputLayout), null, propertyChanged: InputViewChanged);

...

public Label Label
{
get => (Label)GetValue(LabelProperty);
set => SetValue(LabelProperty, value);
}

public InputView InputView
{
get => (InputView)GetValue(InputViewProperty);
set => SetValue(InputViewProperty, value);
}

...

private static void InputViewChanged(BindableObject bindable, object oldValue, object newValue)
{
if (bindable is not InputLayout intputLayout
|| newValue is not InputView inputView
|| inputView is not IFontElement fontElement) return;

Grid.SetColumn(inputView, 2);
Grid.SetColumnSpan(inputView, 1);
Grid.SetRow(inputView, 1);
Grid.SetRowSpan(inputView, 1);
intputLayout.Children.Add(inputView);
}
}
````

We should be able to use it like this, But the binding on the Entry does not work anymore...
````xaml






````

I also tried to create a intermediate Bindable Text Property in my InputLayout, then bind it progamaticaly, but it also doesn not work.
```csharp
InputView.SetBinding(InputView.TextProperty, new Binding(nameof(Text)) { Source = this, Mode = BindingMode.TwoWay });
```

Hope you get it :)

### Steps to Reproduce

1. Take the code below to create a nested control in another one.
2. Add an Entry as the nested control.
3. Try to bind the TextProperty to a stringProperty from the BindingContext.
4. You will never Bind them in anyway (OneWay, TwoWay does not work)..

### Link to public reproduction project repository

Does my sample code is enough? Let me know, Regards

### Version with bug

6.0.486 (current)

### Last version that worked well

Unknown/Other

### Affected platforms

iOS, Android, Windows, macOS

### Affected platform versions

iOS 16, Mac, Android 12

### Did you find any workaround?

You will need to provide a relative Source to make it works.
```csharp
Text="{Binding Source={RelativeSource AncestorType={x:Type vm:SignInViewModel}}, Path=Email, Mode=TwoWay}"/>
```

### Relevant log output

_No response_

Contributor guide

Open the contributing guide

Research direction

The issue names no repository files or tests; start by reproducing the nested InputLayout and Entry example in a .NET MAUI sample, then inspect BindingContext propagation when InputView is added to Children. Done means the Entry Text binding reaches the page view model and TwoWay updates work without the RelativeSource workaround.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
frontend, mobile-dev
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.