waqas waqas
waqas waqas

Reputation: 237

xamarin form : how to change label with stepper

i want to increment or decrement value of label based on stepper value change, but stuck here. Here is my code

<ListView x:Name="mylistview"


  >
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>

                <StackLayout HorizontalOptions="StartAndExpand">

                    <Label HorizontalOptions="Center"   Text="{Binding Qty, StringFormat='Qty. {0:N}'}" FontSize="11"
                   TextColor="Black" />

                    <Stepper ValueChanged="stepper_ValueChanged"   Minimum="0" Maximum="10" x:Name="stepper" Value="{Binding Qty}" Increment="0.1" HorizontalOptions="LayoutOptions.Center" VerticalOptions="LayoutOptions.CenterAndExpand"  />

                </StackLayout>

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

Upvotes: 0

Views: 633

Answers (2)

Junior Jiang
Junior Jiang

Reputation: 12723

1.In the code,if you just want bind label text to stepper you can do this.

<ListView x:Name="mylistview">
<ListView.ItemTemplate>
    <DataTemplate>
        <ViewCell>

            <StackLayout HorizontalOptions="StartAndExpand">

                <Label HorizontalOptions="Center" BindingContext="{x:Refrence Name=stepper}"  Text="{Binding Path=Value, StringFormat='Qty. {0:N}'}" FontSize="11"
               TextColor="Black" />

                <Stepper ValueChanged="stepper_ValueChanged"   Minimum="0" Maximum="10" x:Name="stepper" Increment="0.1" HorizontalOptions="LayoutOptions.Center" VerticalOptions="LayoutOptions.CenterAndExpand"  />

            </StackLayout>

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

just add bindcontext to label,then bind stepper's value,this labe text can be changed by stepper.if have a problem,you can refer to hereofficial document

2,When you use modle bingding data,you should use INotifyPropertyChanged to your modle,if this do not have ,the value can not be changed.

3,when you use modle,according to your code the Qty should be contained to listview's itemsource,not contained just in BindContext,so the Qty can be useful.

Upvotes: 1

hashimks
hashimks

Reputation: 1225

How you can achieve is through below steps, if you are using MVVM

  1. Have a model property for Label (which raises propertychanged event handler) which displays the count.
  2. In the stepper_ValueChanged, get the object (model data) associated with that view cell and in the view model, code it to set/change the count property of label based on the value change.

Upvotes: 0

Related Questions