将Stepper值绑定到相应的listview项的参数-Xamarin.forms

时间:2018-08-16 06:06:35

标签: c# listview xamarin.forms xamarin.forms.listview stepper

我将步进器放在列表视图上,因此每条记录都有一个步进器。我想将步进onchanged值绑定到相应的对象参数值之一。因此,可以通过相应的步进器来更改每个列出的记录的参数。 有可能这样做吗? 这是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>

这是ListView数据的类:

[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; }
}

一个空的OnStepperValueChanged方法:

private void OnStepperValueChanged(object sender, ValueChangedEventArgs e)
    {

    }

我需要步进器来更改可观的NumeroPiatti值。

1 个答案:

答案 0 :(得分:0)

首先,我建议您对实体(Table类)和模型进行分离,将其用于显示页面中的某些内容。它导致编写更多的代码,但更干净,更易于维护,特别是如果您想在此模型中实现INotifyPropertyChanged

如果要遵循MVVM,请确保您的模型实现了INotifyPropertyChanged,还可以创建一些基本模型或使用Fody删除一些样板代码。

其余内容与您在上一条评论中提到的相同,您可以通过以下方式使用Stepper控件:

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

祝您编程愉快!