locked
ListView SelectionChanged Event not fired correctly RRS feed

  • Question

  • Hi,

    I'm trying to some custom changes to the selected item in a ListView. So I added a listener to SelectionChanged. Everything seems to work fine, except when I add a new item to the list and want to select it.
    What I do, is that I add a new model object to the list property and set the selected entry property in the ViewModel. Then I fire the PropertyChangeEvents first for the list, then for the selection.
    The changes get propagated to the UI just fine. My listener is called but I only have an entry in e.RemovedItems but I don't get the newly selected entry in e.AddedItems.

            private void ListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
                if (SelectedItemTemplate != null)
                {
                    if (e.AddedItems != null)
                    {
                        foreach (object item in e.AddedItems)
                        {
                        ...
                        }
                    }
                    if (e.RemovedItems != null)
                    {
                        foreach (object item in e.RemovedItems)
                        {
                        ...
                        }
                    }
                }
            }

    Am I using the wrong event? What am I doing wrong?

    Regards,

    Rico

    Friday, October 31, 2014 3:28 PM

Answers

  • Hi,

    I already tried to use the TemplateSelector, but I cannot get it to choose a new Template for a specific item, when selection changes. I can get it to update all the templates, but that doesn't look good on the screen.

    But I figured out what my problem is. I made the mistake to always create a new list of new model objects in the getter of the property for the element list (I was loading the list from a database). I now have the property as an ObservableCollection, that is modified when needed.

    With that change the events are working as I expect them to and my switching Templates is working smoothly.

    Thanks everyone for looking into it.

    Regards,

    Rico


    Wednesday, November 5, 2014 11:15 AM

All replies

  • Hi,

    alright I figured out 2 things:
    1) That the addedItems list was empty was probably due to an event order issue. I think that the event for the selected element was somehow processed first. Why I cannot say exactly, because it only happens, when the Listview had a selected Item, without a selection it works.
    2) In the event handler I tried to get the ListViewItem for the newly added element. But when the event is fired, but it does not exist yet. So I have to figure out something else, because I want to change the ListViewItem, when it is selected.

    So think my issue has to do with event processing and threading. I didn't really look into that yet. If anybody could point out on where to find out more about that, I would appreciate that.

    Regards,
    Rico
    Sunday, November 2, 2014 4:22 PM
  • Hi Rico,

    >So I have to figure out something else, because I want to change the ListViewItem, when it is selected.

    What would you want to do? Change the liteviewitem’s style when it is selected? Change the data source when lsitviewitem is selected? There is less information for me to understand your problem.

    If you want to change listviewitem style at runtime, see the following link for more information. http://stackoverflow.com/questions/9623938/styling-a-selected-listviewitem-in-windows-8-cp.

    If I misunderstand you, please send me a repro project to help understanding your issue. Use your OneDrive and share a link here.

    Regards,


    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.

    Monday, November 3, 2014 6:02 AM
    Moderator
  • In your ListView, you need to change the swipe gesture setting to the following to enable touch selection:

    IsSwipeEnabled="true"
    

    You should also consider changing the selection mode to single or multiple, to allow an item to be selected:

    SelectionMode="Single"

    You also can refer to


    http://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh943062.aspx

    or my website: cheahengsoon.weebly.com/blog

    Monday, November 3, 2014 6:57 AM
  • Hi,

    I did find similar examples before and wanted to go this route, but all the examples just showed how to change colors. I want to have a completely different template. If the item is not selected, I want to show a quick preview of the element. If it is selected, I want the user to be able to edit all the properties of the element. I didn't find a way to do that with the style.

    Regards,

    Rico


    Monday, November 3, 2014 7:03 AM
  • Hi Rico,

    There is a class named DataTemplateSelector, we can use it to select DataTemplate at runtime according one property in your data source collection. When you set DataTemplateSelector property in ListView XAML, system will use that class to select the appropriate template for each object in data source collection. The code snippet looks like the following.

    public class ListViewItemTemplateSelector : DataTemplateSelector
        {
            public DataTemplate DefaultTemplate { get; set; }
            protected override DataTemplate SelectTemplateCore(object item, DependencyObject container)
            {
                ListViewItemData data = item as ListViewItemData;
                if (data.Selected)
                {
                    string template = @"
                    <DataTemplate xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'>
                            <StackPanel>        
                                <TextBox Text='{Binding Title}' />
                                <TextBox Text='{Binding Content}'  />
                                <TextBox Text='{Binding AddTime}' />
                            </StackPanel>
                    </DataTemplate>";
                    return XamlReader.Load(template) as DataTemplate;
                }
                else {
                    return this.DefaultTemplate;
                }
            }
        }

    You can also refer to this article to see a while code example about how to use DataTemplateSelector class.

    But per my understanding, the things you are going to do will make thing complex. I would recommend you split the edit to other controls like TextBox.

    If you still have questions, please send me a repro project. I will fix this for you. Use your OneDrive and share a link here.

    Regards,

    This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.


    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.


    Wednesday, November 5, 2014 1:47 AM
    Moderator
  • Hi,

    I already tried to use the TemplateSelector, but I cannot get it to choose a new Template for a specific item, when selection changes. I can get it to update all the templates, but that doesn't look good on the screen.

    But I figured out what my problem is. I made the mistake to always create a new list of new model objects in the getter of the property for the element list (I was loading the list from a database). I now have the property as an ObservableCollection, that is modified when needed.

    With that change the events are working as I expect them to and my switching Templates is working smoothly.

    Thanks everyone for looking into it.

    Regards,

    Rico


    Wednesday, November 5, 2014 11:15 AM