The following forum(s) have migrated to Microsoft Q&A (Preview): Developing Universal Windows apps!
Visit Microsoft Q&A (Preview) to post new questions.

Learn More

 locked
[UWP]Retrieving selected Items from ListView to ViewModel RRS feed

  • Question

  • Hello,

    I am still working on my Windows 10 UWP App and an run into one Problem after another. Things who worked on Windows 8.1 Universal, doesn't work on Windows 10 the same way.

    In my Windows 8.1 App the retrieval of selected Items works perfectly. In Windows 10 nothing happens but the converter.

    Has anyone successfully managed to get the selectedItems of a ListView to the ViewModel using Template10 ?

    The Converter is always beeing called, but no Command is invoked.

    Even testing without any Parameter whether a selectionchanged Event does fire something - nothing happens.

    <ListView x:Name="defcon5ListView" Grid.Row="2" ItemTemplate="{StaticResource CheckListEntry}" Background="#B2000000" ItemsSource="{Binding Defcon5CheckList, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" SelectionMode="{Binding DefconListViewSelectionMode}">
                    <Interactivity:Interaction.Behaviors>
                        <Core:EventTriggerBehavior EventName="SelectionChanged">
                            <Core:InvokeCommandAction Command="{Binding SelectionChangedCommand}" InputConverter="{StaticResource SelectedItemsToListConverter}" InputConverterParameter="{Binding ElementName=defcon5ListView}"/>
                        </Core:EventTriggerBehavior>
                    </Interactivity:Interaction.Behaviors>
                </ListView>

    public class SelectedItemsToListConverter : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, string language)
            {
                return (parameter as ListView).SelectedItems;
            }
    
            public object ConvertBack(object value, Type targetType, object parameter, string language)
            {
                return (parameter as ListView).SelectedItems;
            }
        }

    private DelegateCommand<IList<object>> _selectionChangedCommand;
            public DelegateCommand<IList<object>> SelectionChangedCommand
            {
                get
                {
                    if (_selectionChangedCommand != null)
                        return _selectionChangedCommand;
                    _selectionChangedCommand = new DelegateCommand<IList<object>>
                        (
                            (o) =>
                            {
                                foreach (var item in o)
                                {
                                    if (!selectedItems.Contains(item as CheckListItem))
                                    {
                                        selectedItems.Add(item as CheckListItem);
                                    }
                                }
                            }
                        );
                    return _selectionChangedCommand;
                }
            }


    Wednesday, February 17, 2016 2:08 PM

Answers

All replies

  • Event is beeing raised and command invoked only one time at page / Collection initialization.

    Even a simple test app has the same behavior.

    Wednesday, February 17, 2016 5:06 PM
  • After further Investigation, it has something to do whether the command is of type something. This influences whether it will be invoked.

    This means:

    DelegateCommand<IList<object>> SelectionChangedCommand WILL NOT be invoked

    but:

    DelegateCommand SelectionChangedCommand WILL be invoked.

    any ideas how to get this work?

    (It is the Template10 command)

    Wednesday, February 17, 2016 10:35 PM
    • Edited by Marcus Runge Thursday, February 18, 2016 1:59 PM
    • Marked as answer by Marcus Runge Thursday, February 18, 2016 2:00 PM
    Thursday, February 18, 2016 1:58 PM
  • Hi Marcus,

    Well Done!

    I am very glad to know that you have solved your problem by yourself. And thank you very much for sharing your solutions & experience in here.

    Best Regards,
    Amy Peng


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, February 19, 2016 9:59 AM