簡體   English   中英

如何使用 mvvm 管理列表視圖行中按鈕的可見性?

[英]How can I manage the visibility of a button in a listview row using mvvm?

我有一個具有 ViewCell 的列表視圖,在視圖單元格內我有兩個按鈕,當單擊一個按鈕時,它會隱藏其可見性並觸發另一個按鈕的可見性,我面臨的問題是當我單擊一個按鈕時它也更新了其他行中按鈕的可見性。 如何僅更新單擊按鈕的可見性? 這是我所做的:

<ListView.ItemTemplate>
    <DataTemplate>
        <ViewCell>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                     <RowDefinition Height="Auto" />
                </Grid.RowDefinitions>

                <Label Text="{Binding Notes}" Grid.Column="0" Grid.Row="0"/>
                <Button Visual="Material" Text="Start" TextColor="White" 
                        BackgroundColor="#28a745"
                        IsVisible="{Binding Source={x:Reference TimesheetDetailsPage},
                                   Path=BindingContext.IsStartVisible}"
                        Grid.Column="2" Grid.Row="0"
                        Command="{Binding Source={x:Reference TimesheetDetailsPage},
                                 Path=BindingContext.StartCommand}"
                        CommandParameter="{Binding}"/>
                <Button Visual="Material" Text="Stop" TextColor="White" 
                        BackgroundColor="#dc3545"
                        IsVisible="{Binding Source={x:Reference TimesheetDetailsPage},
                                   Path=BindingContext.IsStopVisible}"
                        Grid.Column="2" Grid.Row="0"
                        Command="{Binding Source={x:Reference TimesheetDetailsPage},
                                 Path=BindingContext.StopCommand}"
                        CommandParameter="{Binding}"/> 
            </Grid>
        </ViewCell>
    </DataTemplate>
</ListView.ItemTemplate>

這是我的 viewModel 中的代碼:

public ICommand StartCommand => new Command( async (object item) => {
    TimeSheetTrack myItem = item as TimeSheetTrack;

    IsStartVisible = false;
    IsStopVisible = true;

    UserDialogs.Instance.ShowLoading("Updating timer...");
    await Start(myItem);
    UserDialogs.Instance.HideLoading();

});


public ICommand StopCommand => new Command( async (object item) => {

    TimeSheetTrack myItem = item as TimeSheetTrack;

    IsStartVisible = true;
    IsStopVisible = false;

    UserDialogs.Instance.ShowLoading("Stopping timer...");
    await Stop(myItem);
    UserDialogs.Instance.HideLoading();

});

您已經使用頁面引用了所有按鈕的可見性

IsVisible="{Binding Source={x:Reference TimesheetDetailsPage},
                                   Path=BindingContext.IsStartVisible}"

您需要在項目的 Model 中設置可見性,因此每個項目都有自己的開始和停止按鈕可見性。 因此,如果您這樣做,那么代碼將更改為:-

IsVisible="{Binding IsStartVisible}"

如果您遇到困難,請告訴我。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM