Удалить элемент ListView через DisplayActionSheet - PullRequest
0 голосов
/ 11 марта 2019

Я создаю приложение ListView в Visual Studio 2017 Xamarin.forms, которое показывает мне список моих долгов.Я добавил DisplayActionSheet с веб-сайта Microsoft Xamarin «Отображение всплывающих окон».Как я могу удалить элемент ListView при нажатии через DisplayActionSheet?

Мой DisplayActionSheet выглядит следующим образом:

private async void DebtsList_ItemTapped(object sender, ItemTappedEventArgs e)
    {
        var action = await DisplayActionSheet("Details", "Close", null, "Cash", "Delete","");
        Debug.WriteLine("Action: " + action);
    }

, и вот мой ListView, который показывает мне все мои долги:

<ListView x:Name="DebtsList"
              ItemsSource="{Binding DebtEntries}" 
              CachingStrategy="RecycleElement" 
              Grid.Row="7" 
              Grid.ColumnSpan="3" 
              Grid.RowSpan="15"                  
              HasUnevenRows="True"
              ItemTapped="DebtsList_ItemTapped">
    <ListView.Header>
      <Grid BackgroundColor="White" Margin="7">
        <Grid.ColumnDefinitions>
          <ColumnDefinition Width="*"/>
          <ColumnDefinition Width="*"/>
          <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>

        <Grid.RowDefinitions>
          <RowDefinition Height="auto"/>
        </Grid.RowDefinitions>
        <Label Text="Name" FontSize="11" FontAttributes="Bold" TextColor="#4a4a4a" Grid.Row="0" Grid.Column="0" />
        <Label Text="Usage" FontSize="11" FontAttributes="Bold" TextColor="#4a4a4a" Grid.Row="0" Grid.Column="1" />
        <Label Text="Value" FontSize="11" FontAttributes="Bold" TextColor="#4a4a4a" Grid.Row="0" Grid.Column="2" HorizontalOptions="End"/>
      </Grid>
    </ListView.Header>
    <ListView.ItemTemplate>
      <DataTemplate>
        <ViewCell>
          <Grid BackgroundColor="White" Margin="7">
            <Grid.RowDefinitions>
              <RowDefinition Height="*"/>
              <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
              <ColumnDefinition Width="*"/>
              <ColumnDefinition Width="*"/>
              <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <Label Text="{Binding Name}" 
                     Grid.Row="0"
                     Grid.Column="0"
                     FontSize="10"
                     TextColor="#4a4a4a"/>
            <Label Text="{Binding Usage}" 
                     Grid.Row="0" 
                     Grid.Column="1"
                     FontSize="10"
                     TextColor="#4a4a4a"/>
            <Label Text="{Binding Value}"
                     Grid.Row="0"
                     Grid.Column="2"
                     FontSize="10"
                     TextColor="#F33E3E"                         
                     FontAttributes="Bold"
                     HorizontalOptions="End"/>
            <Label Text="{Binding CreationDate}" 
                     Grid.Row="1" 
                     Grid.Column="0"
                     FontSize="10"
                     TextColor="#4a4a4a"/>
          </Grid>
        </ViewCell>
      </DataTemplate>
    </ListView.ItemTemplate>
  </ListView>

1 Ответ

0 голосов
/ 12 марта 2019

Решение:

private async void ListView_ItemTapped(object sender, ItemTappedEventArgs e)
{
   string result= await  DisplayActionSheet("Details", "Close", null, "Cash", "Delete", "");

   if(result=="Delete")
    {
        int position = DebtsList.TemplatedItems.GetGlobalIndexOfItem(e.Item);

        mySource.RemoveAt(position);

        DebtsList.ItemsSource = mySource;

    }
}

mySource - это ItemsSource в DebtsList, например

public ObservableCollection<DebtEntries> mySource { get; set; }

. . .

mySource = new ObservableCollection<DebtEntries>();
mySource.Add(new DebtEntries { Name = "xxx", Usage="xxx",Value="xxx",CreationDate="xxx"});
//. . .
DebtsList.ItemsSource = mySource;
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...