how to prevent autoselection of first item in GridView on databind?

只愿长相守 提交于 2019-12-24 09:07:24

问题


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
}


来源:https://stackoverflow.com/questions/11419606/how-to-prevent-autoselection-of-first-item-in-gridview-on-databind

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!