首页 > 解决方案 > 将 Stepper 值绑定到相应的列表视图项的参数 - Xamarin.forms

问题描述

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

<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价值。

标签: c#listviewxamarin.formsxamarin.forms.listviewstepper

解决方案


首先,我建议您将实体(表类)和模型分开,您将使用这些模型在页面中显示一些内容。它会导致编写更多代码,但维护起来更干净、更好,特别是如果你想INotifyPropertyChanged在这个模型中实现。

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

其余部分与您在上一条评论中提到的相同,您可以通过Stepper这种方式使用控制:

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

祝你编码好运!


推荐阅读