Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 77050

Remove listview item from a separate page

$
0
0

I have a page with listview and if I click an item on a listview I go to the page with details of a selected item. Here is my page with listview:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:Todo"
             x:Class="Todo.Views.TodoItems"
             xmlns:viewModels="clr-namespace:Todo.ViewModels"
             xmlns:controls="clr-namespace:Todo.Controls;"
             xmlns:suaveControls="clr-namespace:SuaveControls.Views;assembly=SuaveControls.FloatingActionButton"
             Title="Things to do!">
    <ContentPage.BindingContext>
        <viewModels:TodoItemsViewModel />
    </ContentPage.BindingContext>
    <StackLayout>
        <ListView x:Name="listView" Margin="20,20,20,0" ItemsSource="{Binding TodoItems}" HasUnevenRows="True" SeparatorVisibility="None">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <Frame Padding="5" Margin="7" HasShadow="True" MinimumHeightRequest="80">
                            <Grid VerticalOptions="Center" HorizontalOptions="Center">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="2.5*"/>
                                    <ColumnDefinition Width="*"/>
                                    <ColumnDefinition Width="*"/>
                                    <ColumnDefinition Width="1.5*"/>
                                </Grid.ColumnDefinitions>
                                <Label Grid.Column="0" Text="{Binding Name}" VerticalTextAlignment="Center" HorizontalTextAlignment="Start" Margin="25,0,0,0" />
                                <Switch Grid.Column="1" IsToggled="{Binding Done}" HorizontalOptions="Start"/>
                            </Grid>
                        </Frame>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </StackLayout>
</ContentPage>

and here is my page with details of a selected item

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="Todo.Views.AddOrEditItem"
             xmlns:behaviors="clr-namespace:Todo.Behaviors;"
             xmlns:converters="clr-namespace:Todo.Converters;">
    <StackLayout Margin="20">
        <Label Text="Name" />
        <Entry x:Name="name" Text="{Binding Path=Name, Mode=OneWay}">
        </Entry>
        <Button Text="Remove" Command="{Binding RemoveCommand, Source="???"}" />
    </StackLayout>
</ContentPage>

And here is my viewmodel for the page with listview

class TodoItemsViewModel
{
        public ObservableCollection<TodoItem> TodoItems { get; set; }
        public TodoItemsViewModel()
        {
            TodoItems = TodoItemsService.GetTodoItems();
        }
}

I want to follow MVVM pattern and I don't know how to remove the item from the details page. In the Command property it would be great if I could do sth like this:

<Button Text="Remove" Command="{Binding RemoveCommand, Source="TodoItemsViewModel"}" />

so as you can see I want to specify the source of the command to viewmodel of all items, not the viewmodel of a single item. This way I can have easily access to observable collection which I want to modify. But I didn't find any answer how to set the source of binding to concrete class. I found only that you can specify source of binding to another control in template. I will be very grateful for any help.


Viewing all articles
Browse latest Browse all 77050

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>