how to prevent autoselection of first item in Grid

2019-07-26 16:19发布

问题:

When I databind my GridView to an ObservableCollection, the first item of the collection is automatically selected. The SelectionMode property of the GridView is set to multiple. Is there some way to prevent this auto-selection? Or on what event should I listen so that I can reset the SelectedIndex of the GridView back to -1?

回答1:

Set the IsSynchronizedWithCurrenItem property to false on the gridview in xaml



回答2:

There is acutally a pretty simple solution. I set the SelectionMode of the GridView to None in the XAML. Then, when the page is created, I change the SelectionMode to Multiple.

        publicPage()
        {
            this.InitializeComponent();
            itemListView.SelectionMode = ListViewSelectionMode.Multiple;
        }

However, the problem I am having seems to be caused by my own program. This is a workaround for the issue I am having, the autoselection is not the default behavior.

http://social.msdn.microsoft.com/Forums/en-US/winappswithcsharp/thread/da7e9f3b-9a3e-47ca-8223-b50539293f5f



回答3:

My situation is the opposite. I have a GridView that was bind to an ObservableCollection, and I wanted the 1st item to be selected but it was not! I figured out why that was the case though. There are 2 ways to generate my ObservableCollection and depending on which method I chose, the 1st item is either selected or not.

for example, I have a variable ItemList in my viewmodel which I bind to my GridView

public ObservableCollection<Item> ItemList { get; private set; }

Method 1 (nothing selected)

public void getData()
{
    var myList = // get your list here
    for (int i = 0; i < myList.Count; i++)
    {
        this.ItemList.add(myList[i]);
    }
}

Method 2 (1st item automatically selected)

public void getData()
{
    var myList = // get your list here
    this.ItemList = myList
}