2

I built a simple adaptation of the Entry control as follows:

<?xml version="1.0" encoding="utf-8" ?>
<Grid xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  xmlns:local="clr-namespace:B1WApp.CustomControls"
  x:Class="B1WApp.CustomControls.UI5Entry"
  x:DataType="local:UI5Entry">

<Border StrokeShape="RoundRectangle 5" 
        Stroke="{StaticResource SAPFieldBackground}"
        BackgroundColor="{StaticResource SAPFieldBorderColor}"
        Padding="1"
        x:Name="OuterBorder">
    <Border StrokeShape="RoundRectangle 4" 
            Stroke="{StaticResource SAPFieldBackground}"
            BackgroundColor="{StaticResource SAPFieldBackground}">
        <Entry Text="{Binding Path=Text}"
               Placeholder="{Binding Path=Placeholder}"
               PlaceholderColor="{StaticResource SAPFieldPlaceholderColor}"
               ReturnType="{Binding Path=ReturnType}"
               IsPassword="{Binding Path=IsPassword}"
               Margin="{OnPlatform Default='0', Android='0,-8,0,-8'}"
               Focused="Entry_Focused"
               Unfocused="Entry_Unfocused"
               Completed="Entry_Completed"
               ReturnCommand="{Binding Path=ReturnCommand}"/>
    </Border>
</Border>
</Grid>

In code-behind I have:

using System.Windows.Input;

public partial class UI5Entry : Grid
{
    public UI5Entry()
    {
        InitializeComponent();

        this.BindingContext = this;
    }

// additional code omitted for brevity

    public static readonly BindableProperty ReturnCommandProperty = BindableProperty.Create(
        propertyName: nameof(ReturnCommand),
        returnType: typeof(ICommand),
        declaringType: typeof(UI5Entry),
        defaultValue: null,
        defaultBindingMode: BindingMode.OneWay);

    public ICommand ReturnCommand
    {
        get => (ICommand)GetValue(ReturnCommandProperty);
        set => SetValue(ReturnCommandProperty, value);
    }
}

However, when I use the control, the ReturnCommand never fires. The other properties work fine, however. Am I missing something here?

<customControls:UI5Entry 
                ReturnType="Search"
                Placeholder="Digita/escanea # de doc."
                Text="{Binding SearchText, Mode=TwoWay}"
                ReturnCommand="{Binding SearchForBaseDocumentsCommand}"
                />

Thanks in advance for any hints on this issue.

Best regards,

Joerg.

Joerg
  • 177
  • 2
  • 11
  • Put a breakpoint on `ReturnCommand`s `set` line. Is that breakpoint reached? Actually, I'm not sure if XAML will get there; a better test is to add `propertyChanged: (bindable, oldValue, newValue) => { ... }` as last parameter in `ReturnCommandProperty = BindableProperty.Create( ...)`, and put a breakpoint inside that added `{ ... }`. I mean, put some statements where I have `...`, on separate lines. See if it gets into that inline function. – ToolmakerSteve Dec 10 '22 at 04:45
  • Hello @ToolmakerSteve. Thanks for your reply! I tried both, and neither breakpoint is ever hit. [Here's what I did](https://ibb.co/k0Lx2Zs). Does that help point in the direction where the problem is? – Joerg Dec 11 '22 at 15:01
  • I think I sort of found the problem. The issue seems to be the BindingContext. However if I remove `this.BindingContext = this;` from my Control, as suggested [here](https://stackoverflow.com/questions/54493300/xamarin-forms-icommand-bindable-property-is-not-working-on-custom-view), then my other properties (Text, etc.) don't bind anymore... there must be a way handle this I guess...? Here are the [binding errors](https://ibb.co/ckNK19n). – Joerg Dec 11 '22 at 15:47
  • 1
    There is a similar issue on github: [Entry.Completed and Entry.ReturnCommand not executed](https://github.com/dotnet/maui/issues/8960) , you can try the solution there. – Zack Dec 12 '22 at 02:31
  • Thank you @DongzhiWang-MSFT. However, I had already seen that issue and it is different. Also, it has been fixed in 6.0 so it should also not be a problem in 7.0. – Joerg Dec 12 '22 at 13:05
  • *"it has been fixed in 6.0 so it should also not be a problem in 7.0"*, Maybe not. The issue was closed when it was **scheduled** for release, to be synced with .Net 6/7 service releases. I'm not seeing issue #8960 listed as fixed in [Maui release notes](https://github.com/dotnet/maui/releases). Unless that is an omission, this means it didn't make it into last month's update cycle. It should be in the next VS update. – ToolmakerSteve Dec 12 '22 at 21:20
  • Re *"if I remove this.BindingContext = this; ..."*. Two details to understand: **1)** If you remove that, then you have to do an **alternative** mechanism instead, for ALL of your Bindings. You can't just remove something. **2)** Even if you correctly did an alternative, that won't help here, given that the breakpoints are not reached, **tl;dr:** that link you found is not relevant to this situation. Either try what is shown in issue #8960, or wait for next VS update. – ToolmakerSteve Dec 12 '22 at 21:41
  • 1
    Hello @ToolmakerSteve, issue #8960 was merged into [#9632](https://github.com/dotnet/maui/pull/9632), which is in fact listed as fixed in the MAUI release notes for 7.0.0-rc.2.6866. Also, this issue doesn't pertain to custom controls specifically, but rather the Entry control itself, and that binding works perfectly fine. My problem really only happens when passing the binding through a custom control... – Joerg Dec 13 '22 at 15:06
  • OK, that is different. I have an answer that avoids `BindingContext = this;` for custom component: [data binding not carrying through to custom component](https://stackoverflow.com/a/73014571/199364). Add `x:Name="somename"`. Change all `{Binding SomeProperty}` to `{Binding SomeProperty, Source={x:Reference somename}}`. – ToolmakerSteve Dec 14 '22 at 01:09
  • 1
    OK, @ToolmakerSteve. I have tried that suggestion before, when I removed the `BindingContext = this` as part of an earlier comment. However, I guess I will have to try and create a project from scratch with minimum code only, for submission as an issue, and see where I get with that. I'm thinking it might be a bug after all... – Joerg Dec 14 '22 at 15:10
  • *" I have tried that suggestion before, "* Are you sure? All you said was that you removed `BindingContext = this;`. Doing so **broke all your bindings** - you also have to **add** something to tell it where to find the bindings. **Read the rest of my comment,** and the section **ACCESS COMPONENT PROPERTIES VIA x:Name** in the link I gave. – ToolmakerSteve Dec 14 '22 at 22:33
  • Hello @ToolmakerSteve. Yes, I'm aware. As part of removing `BindingContext = this;` I had of course created individual Bindings in the way you describe. However, I will try again, just to be absolutely sure I did not make a mistake there... – Joerg Dec 15 '22 at 12:51
  • @Joerg Did your try again work? – Zack Dec 20 '22 at 01:07

0 Answers0