Custom ViewCell Bindings

dan1229dan1229 Member ✭✭
edited August 2018 in Xamarin.Forms

I'm trying to create a ListView that displays a custom cell I've made (ResultCell) by populating it with the elements of an ObservableList in my ViewModel, said list contains a Model/class I've made.

Long story short, I'm having some issue Binding the values from my Model to my cell, after further debugging I realized it's never calling the "Set" method in any of the Bindable Properties.

I have this in my ResultsPage.xaml:

    <ListView.ItemTemplate>
            <DataTemplate>
                <controls:ResultCell 
                Name="{Binding Name}"
                Description="{Binding Description}"
                Price="{Binding Price}"
                Rating="{Binding RatingImage}"
                Image="{Binding ImageUri}"/>
            </DataTemplate>
        </ListView.ItemTemplate>

Each of the bindings has a corresponding property in my model, I know these bindings are correct as they worked when I was just putting text on the screen and not going through the cell.

In my ResultsCell I have a bunch of bindable properties with this form:

    public static readonly BindableProperty PriceProperty =
        BindableProperty.Create("Price", typeof(string), typeof(ResultCell), "$");

    public string Price
    {
        get { return (string)GetValue(PriceProperty); }
        set { SetValue(PriceProperty, value); }
    }

I also have a OnBindingContextChanged():

    protected override void OnBindingContextChanged()
    {
        base.OnBindingContextChanged();

        if (BindingContext != null)
        {
            name.Text = Name;
            price.Text = Price;
            description.Text = Description;

            placeImage.Source = Image;
            ratingImage.Source = Rating;
        }
    }

For whatever reason when I get rid of this method, some of the bindings work though. The cells construct properly and bind all the properties to the right views within the cell, however it refuses to bind to the actual values in the model now. Again, after further debugging I've realized that the bindings I set in the actual page are never being set on their corresponding properties in the cell (the setter methods are never being called).

Thanks in advance for the help!

Answers

  • JGoldbergerJGoldberger USMember, Forum Administrator, Xamarin Team, University Xamurai

    @dan1229

    First, moving this to the Xamarin.Forms category.

    I need to see some more code to understand what is going on here. Seeing the complete Model, View Model, Page, and ResultCell would help. If you could put together a simple, publicly share-able (i.e. no sensitive code or data) test project that demonstrates the issue and explain exactly the steps to reproduce the issue including the expected behavior vs the actual behavior as I a not clearly understanding your description above. Mainly not sure where and when your properties are being set. Is your ResultCell data editable, IOW are you using Entry's for name, price, etc? Are you expecting that typing a new name in the name field will update your ViewModel and Model?

Sign In or Register to comment.