[Bug] BindingContext default behavior, on nested controls
See original GitHub issueDescription
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
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…
<controls:InputLayout x:Name="_email" StyleClass="NormalInputLayout">
<controls:InputLayout.Label>
<Label Text="Email"/>
</controls:InputLayout.Label>
<controls:InputLayout.InputView>
<Entry
Keyboard="Email"
IsTextPredictionEnabled="false"
Text="{Binding Email, Mode=TwoWay}"/>
</controls:InputLayout.InputView>
</controls:InputLayout>
I also tried to create a intermediate Bindable Text Property in my InputLayout, then bind it progamaticaly, but it also doesn not work.
InputView.SetBinding(InputView.TextProperty, new Binding(nameof(Text)) { Source = this, Mode = BindingMode.TwoWay });
Hope you get it 😃
Steps to Reproduce
- Take the code below to create a nested control in another one.
- Add an Entry as the nested control.
- Try to bind the TextProperty to a stringProperty from the BindingContext.
- 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.
Text="{Binding Source={RelativeSource AncestorType={x:Type vm:SignInViewModel}}, Path=Email, Mode=TwoWay}"/>
Relevant log output
No response
Issue Analytics
- State:
- Created a year ago
- Comments:5
Top Results From Across the Web
DefaultModelBinder Problem with nested levels + other ...
Country returns a Country class instance (ISO2/3/Name/Code logic). It is not a string. Not surprise that it doesn't work by default. My first ......
Read more >Data binding in depth - UWP applications
A binding object has a Source property, which defaults to the DataContext of the UI element on which the binding is declared. You...
Read more >What's new in Windows Forms in .NET 7.0
Most notably, the Windows Forms runtime now can: Correctly scale nested controls (e.g., a button which resides in panel, which itself is placed ......
Read more >Controls aren't validated when placed on invisible tab pages
This is default behavior powered by Microsoft and doesn't relate to our components. To resolve this issue, you can iterate through each page...
Read more >The "with" and "using" bindings
The with and using bindings create a new binding context, so that descendant elements are bound in the context of a specified object....
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

I am not sure if this should work or not but you can always use relative bindings in that case
This is kind a Sucks, The Intel sense does show the properties for nested controls and binds without any issue but doesn’t work when app launches, On Most Cases We need to use RelativeSource or Reference Markup Extensions to Get Most of it work on Nested Controls with Compiled Bindings with Single Data Context