none
ListView multiselect ungly behavior RRS feed

  • Question

  •  I have write a simple code to test listview controls' multiselect behavior, see below:
    <Window.Resources> 
            <local:People x:Key="family">  
                <local:Person  Name="Anders" Age="20"/>  
                <local:Person  Name="Kavin" Age="22"/>  
                <local:Person  Name="Candy" Age="10"/>  
                <local:Person  Name="Rich" Age="40"/>  
            </local:People> 
        </Window.Resources> 
        <Grid> 
            <StackPanel > 
            <Button Name="addBtn" Click="addBtn_Click" >Add</Button> 
            <ListView Name="listView1"  ItemsSource="{StaticResource family}"/>  
            </StackPanel> 
        </Grid> 
    private void addBtn_Click(object sender, RoutedEventArgs e)  
    {  
        People family = this.Resources["family"] as People;  
        if (family != null)  
        {  
            Person p = new Person() { Name = "older"Age = 100 };  
            family.Add(p);  
            listView1.SelectedItem = p;     
        }  
          
    }  
     

    【Steps】

    1.Run the application, select the second item-peson Kavin

    2.Click add button, add a new peson

    3.Press Shift, then select first peson-Anders, 

    The result is  the top 2 people have been selected, but the expect result is  all of the people should be selected.

     

    Is this a  listview bug, does anyone know how to solve this problem.

    Thanks!


    anders06
    • Edited by anders06 Monday, August 11, 2008 8:52 AM modify
    Monday, August 11, 2008 8:30 AM

Answers

  • I think this might be a bug with  ListView/ListView's selection logic, although extended selection should be done by user interaction aka though keyboard and mouse, but it would be better if WPF provides a way to enfluence the extended selection mechanism, since when a item is selected programmatically, the chance is that the end user wants to make it as the anchor item (aka starting item when doing extended selection). To workaround this "design" noise, the "only" way I find is use reflection to set the internal "AnchorItem" property as follows:

    private void addBtn_Click(object sender, RoutedEventArgs e)
    {
        People family = this.Resources["family"] as People;
        if (family != null)
        {
            Person p = new Person() { Name = "older", Age = 100 };
            family.Add(p);
            listView1.SelectedItem = p;

            // Note this method will break since we reply on the internal API to do the trick, considering this technique very carefully when deciding to use it in production code.
            PropertyInfo pi = typeof(ListBox).GetProperty("AnchorItem", BindingFlags.NonPublic | BindingFlags.Instance);
            if (pi != null)
            {
                pi.SetValue(listView1, p, null);
            }
            listView1.Focus();
        }
    }

    I would greatly appreciate it if you could file a bug on this the connect site:

    https://connect.microsoft.com/feedback/default.aspx?SiteID=212&wa=wsignin1.0


    Hope this helps
    • Marked as answer by Marco Zhou Monday, August 18, 2008 10:10 AM
    Monday, August 18, 2008 5:09 AM

All replies

  •  Can anyone help me
    anders06
    Tuesday, August 12, 2008 1:42 AM
  • -> Is this a  listview bug, does anyone know how to solve this problem.

    Could you please provide a small, complete and ready-to-run example to demonstrate the issue you are encountering?

     

    Thanks


    Wednesday, August 13, 2008 7:16 AM
  • Hi Marco,
    Thanks for your reply.

    The following is whole code:
    [Xmal]

    <Window x:Class="ListViewMultiSelect.Window1" 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        xmlns:local="clr-namespace:ListViewMultiSelect" 
        Title="Window1" Height="300" Width="300">  
        <Window.Resources> 
            <local:People x:Key="family">  
                <local:Person  Name="Anders" Age="20"/>  
                <local:Person  Name="Kavin" Age="22"/>  
                <local:Person  Name="Candy" Age="10"/>  
                <local:Person  Name="Rich" Age="40"/>  
            </local:People> 
        </Window.Resources> 
        <Grid> 
            <StackPanel > 
                <Button Name="addBtn" Click="addBtn_Click" >Add</Button> 
                <ListView Name="listView1"  ItemsSource="{StaticResource family}"/>  
            </StackPanel> 
        </Grid> 
    </Window> 
    [behind code]
    using System;  
    using System.Collections.Generic;  
    using System.Linq;  
    using System.Text;  
    using System.Windows;  
    using System.Windows.Controls;  
    using System.Windows.Data;  
    using System.Windows.Documents;  
    using System.Windows.Input;  
    using System.Windows.Media;  
    using System.Windows.Media.Imaging;  
    using System.Windows.Navigation;  
    using System.Windows.Shapes;  
    using System.Collections.ObjectModel;  
     
    namespace ListViewMultiSelect  
    {  
        /// <summary> 
        /// Interaction logic for Window1.xaml  
        /// </summary> 
        public partial class Window1 : Window  
        {  
            public Window1()  
            {  
                InitializeComponent();  
            }  
     
            private void addBtn_Click(object sender, RoutedEventArgs e)  
            {  
                People family = this.Resources["family"] as People;  
                if (family != null)  
                {  
                    Person p = new Person() { Name = "older"Age = 100 };  
                    family.Add(p);  
                    listView1.SelectedItem = p;  
                }      
            }  
        }  
     
        public class Person  
        {  
            public string Name { get; set; }  
            public int Age { get; set; }  
     
            public override string ToString()  
            {  
                return Name;  
            }  
        }  
     
        public class People : ObservableCollection<Person> 
        {  
        }  
    }  
     

    Note that when add a new item, the item will be selected.

    [Steps to Recreate]
    1. Run the application,  click the second item, so the person named "Kavin" has been selected

    2. Click add button (the last item has been add and selected)

    3. Press Shift key,  click the first item.        The result is only top 2 people have been selected.       

    My expect result is all of the people should be selected. Because after step 2, the last item has been selected, not the second item.

    How can i solve this problem and meet to my desired reslut (all of people have been selected after step 3)?

    Thanks


    anders06
    Thursday, August 14, 2008 2:35 AM
  • I think this might be a bug with  ListView/ListView's selection logic, although extended selection should be done by user interaction aka though keyboard and mouse, but it would be better if WPF provides a way to enfluence the extended selection mechanism, since when a item is selected programmatically, the chance is that the end user wants to make it as the anchor item (aka starting item when doing extended selection). To workaround this "design" noise, the "only" way I find is use reflection to set the internal "AnchorItem" property as follows:

    private void addBtn_Click(object sender, RoutedEventArgs e)
    {
        People family = this.Resources["family"] as People;
        if (family != null)
        {
            Person p = new Person() { Name = "older", Age = 100 };
            family.Add(p);
            listView1.SelectedItem = p;

            // Note this method will break since we reply on the internal API to do the trick, considering this technique very carefully when deciding to use it in production code.
            PropertyInfo pi = typeof(ListBox).GetProperty("AnchorItem", BindingFlags.NonPublic | BindingFlags.Instance);
            if (pi != null)
            {
                pi.SetValue(listView1, p, null);
            }
            listView1.Focus();
        }
    }

    I would greatly appreciate it if you could file a bug on this the connect site:

    https://connect.microsoft.com/feedback/default.aspx?SiteID=212&wa=wsignin1.0


    Hope this helps
    • Marked as answer by Marco Zhou Monday, August 18, 2008 10:10 AM
    Monday, August 18, 2008 5:09 AM
  • Thank you very mush.
    You are superman.
    anders06
    Tuesday, August 19, 2008 3:15 AM