Forum Xamarin.Forms

XAML, DataBinding and local database

AymericAymeric FRMember ✭✭

Hey, I've a very basic app with two tables : Product (name, quantity) and Category.

When selecting a product from a ListView, I want to display its Name, Quantity and Category. The Product's Category is registred as the ID of the Category. I want to display its Category's Name rather than its ID.

Making it in pure code, I could easily make a method for getting the category name associated to an ID, however in my case I'm using XAML. And I don't really know how to achieve it. What is the best way to handle this?

<ContentPage xmlns="" xmlns:x="" x:Class="ProductXAML" Title="Product">
    <StackLayout VerticalOptions ="FillAndExpand">
        <StackLayout Orientation="Horizontal">
            <Label Text="Name :" />
            <Label Text="{Binding Name}" />

        <StackLayout Orientation="Horizontal">
            <Label Text="Quantity :" />
            <Label Text="{Binding Quantity}" />
            <Stepper x:Name="quantity" Minimum="0" Increment="1" Value="{Binding Quantity}" IsVisible="false" />

        <StackLayout Orientation="Horizontal">
            <Label Text="Category :" />
            <Label Text="{Binding Category}" />


  • PaulDistonPaulDiston USUniversity ✭✭✭✭


    One way to achieve this is to add a new property in your ViewModel, which would allow for a call to the database to populate the Category based on the Id you have defined.

    Another way to achieve this is when creating your ProductViewModels, you will have a Product and a Category from the database, you can then create a ProductViewModel based on these two Model objects.

    Just a note, a ViewModel does't have to have a one to one relationship with your Model objects, you can as suggested above combine models to produce a more complete ViewModel object.

    Hope this helps.

    Paul Diston

Sign In or Register to comment.