Search code examples
c#listviewxamarin.formsxamarin.forms.listviewstepper

Binding Stepper value to the corresponding listview item's parameter - Xamarin.forms


I put the stepper on listview so each record has one stepper. I would like to bind the stepper onchanged value to one of the corresponding objects parameter value. So each listed records' parameter can be altered by corresponding stepper. Is it possible to do so? Here is the ListView:

<ListView x:Name="TempMenuListView" HasUnevenRows="true" Grid.Row="2" SeparatorColor="Black" >
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <Grid  Padding="10">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="*"/>
                            <ColumnDefinition Width="*"/>
                        </Grid.ColumnDefinitions>
                        <Label Text="{Binding PiattoTipo}" Grid.Row="0" Grid.Column="0" TextColor="Black" />
                        <Label Text="{Binding Piatto}" Grid.Row="0" Grid.Column="1" TextColor="Black" />
                        <Label x:Name="numeroPiattiLabel" Text="{Binding NumeroPiatti}" Grid.Row="0" Grid.Column="2" TextColor="Black" />
                        <Stepper x:Name="stepper" Maximum="20" ValueChanged="OnStepperValueChanged"/>

                    </Grid>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

Here is the class for ListView Data:

[Table("TempMenu")]
public class TempMenu
{
    [PrimaryKey, AutoIncrement]
    public int Id { get; set; }
    public DateTime Date { get; set; }
    public int TavoloNo { get; set; }
    public string PiattoTipo { get; set; }
    public string Piatto { get; set; }
    public int NumeroPiatti { get; set; }
}

An empty OnStepperValueChanged method:

private void OnStepperValueChanged(object sender, ValueChangedEventArgs e)
    {

    }

I need the stepper to change respectable NumeroPiatti value.


Solution

  • First of all I would suggest you to do separation of your Entity (Table class) and models which you will use to show some content in your pages. It leads to writing more code but it is cleaner and nicer to maintain, especially if you want to implement INotifyPropertyChanged in this models.

    If you want to follow the MVVM, make sure that your model implements INotifyPropertyChanged you can also make some base model or use Fody to remove some boilerplate code.

    Rest of it is same as you mentioned in your last comment you can use Stepper control in this way:

    //... rest of your code XAML
    <Stepper x:Name="stepper" Maximum="20" Value={Binding SomePropertyInModel} />
    //... rest of your code XAML
    

    Wishing you lots of luck with coding!