0
votes

I have a custom control based on ContentView with some BindableProperty fields. These controls work great on normal ContentPage pages.

I am now trying to use them in a popup, using the Rg.Plugins.Popup nuget package. The controls display normally, but they never show any values - the binding does not appear to be working. The fields do appear to get bound to null before the constructor of the popups ContentView is called, but nothing happens when the BindingContext is changed.

Here is how the BindableProperty(s) are set up in the control:

public string FieldValue { get; set; }
public static readonly BindableProperty FieldValueProperty = BindableProperty.Create(
                                                             propertyName: "FieldValue",
                                                             returnType: typeof(string),
                                                             declaringType: typeof(MFGField),
                                                             defaultValue: "",
                                                             defaultBindingMode: BindingMode.TwoWay,
                                                             propertyChanged: FieldValuePropertyChanged);

private static void FieldValuePropertyChanged(BindableObject bindable, object oldValue, object newValue)
{
    var control = (MFGField)bindable;
    if(newValue != null)
        control.ValueEntry.Text = newValue.ToString();
}

Here is how it is set up in the XAML:

<MyNamespace:MFGField x:Name="OrderNumber" FieldValue="{Binding CurrentPick[Order_Number]}" LabelText="Order Number:" ReadOnly="true" Grid.Column="0" Grid.Row="0"/>

The popup ContentView currently has a ViewLifecycleEffect that serves as the 'OnAppearing' for the popup, and that is currently where I am setting the BindingContext after loading the data:

        private async void ViewLifecycleEffect_OnLoaded(object sender, EventArgs e)
    {
        await callingPage.LoadBinRecord(callingPage.SearchTextValue);
        ((AppData)this.BindingContext).CurrentPick = ((AppData)this.BindingContext).CurrentPicks[((AppData)this.BindingContext).PickIndex];
    }

I have confirmed that the data in the object that should be bound is present and correct, but the PropertyChanged functions for my fields don't get called when the BindingContext is updated.

I am coming back to Xamarin Forms after some time, and I'm sure I'm missing something obvious, but I am missing it nevertheless. Any help is appreciated.

1
Could you please share a basic demo so that we can test with it?Jessie Zhang -MSFT

1 Answers

0
votes

My apologies, this issue appears to be the result of my unfortunate choice of fields to which to bind. There are well over 200 fields in the object, and I happened to select several quite reasonable choices for testing, where the values are unexpectedly null.

Initially, I did have a problem getting the BindingContext set properly, and the field issue prevented me from seeing that I had actually fixed that problem. The binding does in fact seem to be working now that the correct fields are bound.