question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Bug] BindingContext default behavior, on nested controls

See original GitHub issue

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

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

  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.

Text="{Binding Source={RelativeSource AncestorType={x:Type vm:SignInViewModel}}, Path=Email, Mode=TwoWay}"/>

Relevant log output

No response

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
SarthakGzcommented, Sep 24, 2022

I am not sure if this should work or not but you can always use relative bindings in that case

0reactions
AathifMahircommented, May 3, 2023

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found