Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 77050

Binding: Property not found

$
0
0

Hello,
I've been working on an app that can build a form( list of fields) from a given json value.
My FieldItem class contains a couple of properties, but for the purpose of this question only two are relevant. the field item contains a string property called Name and a bool property called Required.

To format and display the FieldItem I pass it to a class that inherits from BaseFieldViewModel which contains two methods:

protected abstract View GetViewInstance();
public View GetView(){
    var vw = GetViewInstance();
    if (vw == null) return null;
    vw.BindingContext = this;
    return vw;
}

For simple text type fields like 'text', 'number', 'email', 'phone', 'url' there is a sub class called StringBaseFieldViewModel that extends BaseFieldViewModel
and defines GetViewInstance:

protected override View GetViewInstance() { return new StringFieldView(); }

StringFieldView is a ContentView that does not have anything in the code behind, but has an xaml body containing:

<controls:FieldLabel IsReq="True" Lab="No Binding" />
<Entry Text="" Keyboard="{Binding KeyboardType}" />

FieldLabel is another ContentView with an xaml body of:

<Label Text="{Binding Label}" />
<Label Text="*" IsVisible="{Binding IsRequired}" TextColor="Red" />

the code behind has two bindable properties and a view model definition:

public string Lab { /* ... */ }  
public bool IsReq { /* ... */ }  
public FieldLabelViewModel ViewModel { get; set; } = new FieldLabelViewModel();  

The static BindableProperty fields have their propertyChanged parameter set to a method that assigns the given value to the view model.

FieldLabelViewModel has the properties: Label(string) and IsRequired(bool)
Currently I am setting the binding context for FieldLabel inside the constructor to ViewModel

Finally, to actually pass in and display a field Item for a simple text type is a class called TextFieldViewModel that inherits from StringBaseFieldViewModel (This class sets the keyboard value to text)

I have a problem trying to create an instance of TextFieldViewModel with a FieldItem and call GetView() the result is an empty label for the FieldLabel contentView. I've tried to inspect it but the binding never seems to occur. If i look in the output window I see the following error messages:

[0:] Binding: 'Name' property not found on 'BindingIssue.ViewModels.FieldLabelViewModel', target property: 'BindingIssue.Controls.FieldLabel.Lab'
[0:] Binding: 'Name' property not found on 'BindingIssue.ViewModels.FieldLabelViewModel', target property: 'BindingIssue.Controls.FieldLabel.Lab'

I have also tried creating a FieldLabel with the Lab and IsReq properties manually set to see if the problem was with the FieldLabel or the StringFieldView, but it worked as expected:

<controls:FieldLabel IsReq="True" Lab="No Binding" />

This would make it seem that the issue is then inside the StringFieldView, but I have tried this:

<Label Text="{Binding Name}" />
<controls:FieldLabel IsReq="{Binding IsRequired}" Lab="{Binding Name}" />

The plain label displays correctly, but the FieldLabel's Lab value is still null. From the error message I found in the output window it appears to be looking for the Name property in the FieldLabelViewModel, but I have no idea why.

Would someone be able to help me figure out this issue?

I have attached a visual studio solution that reproduces the problem if that helps.


Viewing all articles
Browse latest Browse all 77050

Trending Articles