Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to implement item selection on HorizontalListView? #8

Open
ske66 opened this issue Apr 23, 2020 · 6 comments
Open

How to implement item selection on HorizontalListView? #8

ske66 opened this issue Apr 23, 2020 · 6 comments
Labels
question Further information is requested

Comments

@ske66
Copy link

ske66 commented Apr 23, 2020

The Drag and Drop functionality is great, but why isn't there any SelectedItem property? Id say this is one of the most important properties of a ListView (apart from ItemSource).

@ske66 ske66 changed the title SelectedItem Property Binding SelectedItem Property Binding on HorizontalListView Apr 23, 2020
@roubachof
Copy link
Owner

You can easily implement it with the TapCommand by adding an IsSelected property in your view model.

@ske66
Copy link
Author

ske66 commented Apr 23, 2020

Do you have any examples?

@roubachof
Copy link
Owner

Well as I said add a IsSelected property in your items ViewModel (let's call them ItemViewModel).
Then bind an ICommand to the TapCommand of the HorizontalListView (let's call it ListTapCommand).
In ListTapCommand just implement the selection logic of your view model items:

class ItemViewModel
{
	public bool IsSelected { get; set; }
}

class ListViewModel
{
	public ListViewModel()
	{
		ListTapCommand = new Command(OnListTap);
	}

	public ObservableCollection<ItemViewModel> Items { get; }

	public ICommand ListTapCommand { get; }

	public void OnListTap(ItemViewModel tappedItem)
	{

		// One and only one selectable element
		foreach (var item in Items)
		{
			item.IsSelected = item == tappedItem;
		}

		// OR
		// One or zero selectable element
		foreach (var item in Items)
		{
			item.IsSelected = item == tappedItem ? !item.IsSelected : false;
		}

		// OR
		// Multiple selection 
		tappedItem.IsSelected = !tappedItem.IsSelected;
	}
}

@ske66
Copy link
Author

ske66 commented Apr 23, 2020

ah thank you. This helps us out a lot.

@ske66 ske66 closed this as completed Apr 23, 2020
@roubachof
Copy link
Owner

I will leave this open since it could help others as well.

@roubachof roubachof reopened this Apr 24, 2020
@roubachof roubachof changed the title SelectedItem Property Binding on HorizontalListView Implement item selection on HorizontalListView Apr 24, 2020
@roubachof roubachof changed the title Implement item selection on HorizontalListView How to implement item selection on HorizontalListView? Apr 24, 2020
@ske66
Copy link
Author

ske66 commented Apr 24, 2020

Actually thanks for re-opening this. Just wanted to include a full code snippet for anyone in the future. Please correct me if my implementation is incorrect.

ListPage.cs

     <renderedViews:HorizontalListView
            ColumnCount="1"
            ListLayout="Grid"
            EnableDragAndDrop="True"
            ItemHeight="80"
            ItemSpacing="2"
            Margin="8"
            CurrentIndex="0"
            TapCommand="{Binding ListTapCommand}"
            VerticalOptions="FillAndExpand" ItemsSource="{Binding ListItems}">
        <renderedViews:HorizontalListView.ItemTemplate>
            <DataTemplate>
                <renderedViews:DraggableViewCell IsDraggable="True" x:Name="DraggableViewCell" >
                    <ContentView>
                        <renderedViews:MaterialFrame Margin="4"
                                     Elevation="4">
                            <Frame.Triggers>
                                <DataTrigger Binding="{Binding Source={x:Reference DraggableViewCell}, Path=IsDragAndDropping}"
                                         TargetType="renderedViews:MaterialFrame"
                                         Value="True">
                                    <Setter Property="Elevation" Value="8" />
                                </DataTrigger>
                            </Frame.Triggers>
                            <StackLayout Orientation="Horizontal" Margin="16" VerticalOptions="Center">
                                <ui:MaterialLabel Text="{Binding Name}" VerticalOptions="CenterAndExpand" HorizontalOptions="StartAndExpand" />
                                <Image Source="printer.png" HorizontalOptions="EndAndExpand"/>
                            </StackLayout>
                        </renderedViews:MaterialFrame>
                    </ContentView>
                </renderedViews:DraggableViewCell>
            </DataTemplate>
        </renderedViews:HorizontalListView.ItemTemplate>
    </renderedViews:HorizontalListView>`

ListPageViewModel.cs

public class ListPageViewModel
    {
        private ObservableCollection<MyListItemModel> ListItems = new ObservableCollection<MyListItemModel>();

        public ObservableCollection<MyListItemModel> ListItems
        {
            get => _strains;
            set => SetProperty(ref _strains, value);
        }

        public ICommand ListTapCommand { get; }

        public ListPageViewModel()
        {
            ListTapCommand = new Command(OnListTap);
            
            ListItems.Add(new MyListItemModel { Name = "John Doe" });
            ListItems.Add(new MyListItemModel { Name = "Jane Doe" });
        }

        public void OnListTap(object obj)
        {
            var item = (MyListItemModel) obj;

            item.IsSelected = !item.IsSelected;
        }

MyListItemModel.cs

    public class MyListItem
    {
        public string Name { get; set; }
        public bool IsSelected { get; set; }
    }

@roubachof roubachof transferred this issue from roubachof/Sharpnado.Presentation.Forms Nov 18, 2020
@roubachof roubachof added the question Further information is requested label Jan 22, 2021
@roubachof roubachof pinned this issue Feb 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants