This is a migrated thread and some comments may be shown as answers.

How to make the 1st row be a default SelectedItem in MVVM or in XAML property?

1 Answer 50 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Rob Ainscough
Top achievements
Rank 1
Rob Ainscough asked on 12 Nov 2014, 10:14 PM
I was hoping there would be a easy way to automatically set the first row as "selected" using SelectedIndex="1" in my XAML ... but I can't see to find such a method.   The only thing I found was considerable code to setup a behavior here: https://github.com/telerik/xaml-sdk/tree/master/GridView/BindingSelectedItemsFromViewModel

I really don't want to write that much code for such a simple task ... all I really want to do is set the first row as selected once data source is loaded/set (which should trigger a selectionchanged which my MVVM code will pickup using Interactivity DLLs.

In a windows forms app, this takes 1 short line of code ... in SL this same task seems to take about 300 lines of code?

I'm hoping I'm wrong and there is an easy MVVM way or even just a property in my XAML for the GridView.

Cheers, Rob.

1 Answer, 1 is accepted

Sort by
0
Dimitrina
Telerik team
answered on 14 Nov 2014, 12:08 PM
Hello Rob,

The case is that initially there are still not items loaded, consequently the SelectedItem cannot be connected with the GridViewRow corresponding to the particular item. You could subscribe for the DataLoaded event of RadGridView and select the desired item as soon as the data in the control is loaded.

You will be able to assign it once it is actually loaded.

For example, you can assure the item is already available with the following code:

void RadGridView1_DataLoaded(object sender, System.EventArgs e)
{
    var item4 = RadGridView1.Items[3];
    if (RadGridView1.Items.Contains(item4))
    {
        RadGridView1.SelectedItem = item4;
    }
}

Let me know how this works for you.

Regards,
Dimitrina
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
GridView
Asked by
Rob Ainscough
Top achievements
Rank 1
Answers by
Dimitrina
Telerik team
Share this question
or