Behavior Binding Issue

bootcombootcom Member ✭✭

Hi,

I'm having (what I consider to be) a really wierd problem.

I have a behavior which is attached to an entry (at the bottom as I can't post links :-)). The behavior gives a boolean value and based on that value I set an image next to an entry.

If I am working on a new record, this works brilliantly and an image displays next to the entry which shows whether it is valid or not.

When I am working on an existing record the image only intermittently shows the success image next to the entry. The wierd bit for this here is that the success image will only show when I cut the value and paste it back in and I'm struggling to understand why.

I create my entry / behaviors directly in c# and not in xaml (not that I'm expecting this to have made any difference)

var itm = await PropertyTypeToEditor(prpRecord.PropertyType, prpRecord.PropertyName, prpRecord.ReadOnly) as Xamarin.Forms.View;

(which gives me the following:
EditorType = new Entry();
((Entry)EditorType).SetBinding(Entry.TextProperty, $"Data.{PrpName}", BindingMode.TwoWay);
)

            itm.VerticalOptions = LayoutOptions.CenterAndExpand;
            itm.HorizontalOptions = LayoutOptions.FillAndExpand;
            itm.Behaviors.Clear();

var NonEmptyStringValidatorBehavior = new Behaviors.NonEmptyStringValidatorBehavior();

                var imgValidation = new Image() { HeightRequest = 40, WidthRequest = 40 };
                imgValidation.BindingContext = NonEmptyStringValidatorBehavior;
                imgValidation.SetBinding(Image.SourceProperty, new Binding() { Converter = ((DataViewPage)this.CurrentPage).boolToStyleImage, Path = "IsValid" });
                imgValidation.VerticalOptions = LayoutOptions.CenterAndExpand;
                imgValidation.HorizontalOptions = LayoutOptions.Center;

                itm.Behaviors.Add(NonEmptyStringValidatorBehavior);

StackLayout.Children.Add(imgValidation);
StackLayout.Children.Add(itm);

I've googled and haven't come up with anything thus far, would anyone be able to give me any hints?

Thanks

Chris

public class NonEmptyStringValidatorBehavior : Behavior
{

    static readonly BindableProperty IsValidProperty = BindableProperty.Create("IsValid", typeof(bool), typeof(NonEmptyStringValidatorBehavior), false);


    internal Entry AssociatedObject;

    public bool IsValid { get => (bool)GetValue(IsValidProperty); set { SetValue(IsValidProperty, value); } }

    internal void OnBindingContextChanged(object sender, EventArgs e)
    {
        this.OnBindingContextChanged();
    }

    protected override void OnBindingContextChanged()
    {
        base.OnBindingContextChanged();
        BindingContext = AssociatedObject.BindingContext;
    }

    protected override void OnAttachedTo(Entry bindable)
    {
        base.OnAttachedTo(bindable);
        AssociatedObject = bindable;
        bindable.BindingContextChanged += OnBindingContextChanged;
        bindable.TextChanged += bindable_TextChanged;
    }

    private void bindable_TextChanged(object sender, TextChangedEventArgs e)
    {
        IsValid = (e.NewTextValue.Length > 0);
    }

    protected override void OnDetachingFrom(Entry bindable)
    {
        base.OnDetachingFrom(bindable);
        bindable.TextChanged -= bindable_TextChanged;
        bindable.BindingContextChanged -= OnBindingContextChanged;
    }

}

Best Answers

  • bootcombootcom ✭✭
    Accepted Answer

    Actually, the demo helped me diagnose the issue - the issue isn't with the behavior. Instead the issue is with the way that the isValid does (or in my case doesn't) seem to work which gives me a better direction to work from.

    Thanks for responding to my question though!

  • bootcombootcom ✭✭
    Accepted Answer

    The behavior works fine. Although I'm pretty sure the stuff I did with the behavior I could have done with a convertor.

    My models that I use are from a seperate library which is shared with a web api (preventing duplication of code in the api and in the xamarin app). In the end I put in a read-only property into said model which then does the same check and the image shows correctly when that's changed. When the value of the property bound to the entry changes it fires off a value change on the new property.

Answers

  • yelinzhyelinzh Member, Xamarin Team Xamurai

    I've reproduced the code but failed to deployed. Could you post a basie demo ?

  • bootcombootcom Member ✭✭

    Thanks. I'm just knocking up a demo project to show this behavior.

  • bootcombootcom Member ✭✭
    Accepted Answer

    Actually, the demo helped me diagnose the issue - the issue isn't with the behavior. Instead the issue is with the way that the isValid does (or in my case doesn't) seem to work which gives me a better direction to work from.

    Thanks for responding to my question though!

  • bootcombootcom Member ✭✭
    Accepted Answer

    The behavior works fine. Although I'm pretty sure the stuff I did with the behavior I could have done with a convertor.

    My models that I use are from a seperate library which is shared with a web api (preventing duplication of code in the api and in the xamarin app). In the end I put in a read-only property into said model which then does the same check and the image shows correctly when that's changed. When the value of the property bound to the entry changes it fires off a value change on the new property.

Sign In or Register to comment.