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

Set Selected Item after data is loaded

5 Answers 186 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Alan
Top achievements
Rank 1
Alan asked on 27 Nov 2012, 08:36 PM
I am using a GridView in a master - detail, MVVM, Prism scenario.  When I navigate to the master, I want to set the selected item to an item whose ID is passed in during navigation.  The difficulty I am having is that appears that the Selected Item is being set in the modelview before the GridView data loading is complete.  In debug, I can see that the Selected Item is getting set properly and PropertyChanged is raised for the operation.  Subsequent to this, the Selected Item is getting reset to a different value from the GridView via the TwoWay binding, I'm guessing after data loading has been completed by the GridView.  How can I ensure that the selection being made from within the viewmodel does not get overriden by the loading operation of the GridView?  Here are some code snippets:
public void OnNavigatedTo(NavigationContext navigationContext)
{
    // Called to initialize views during navigation.
 
    // Is this for AgentView?
    if (navigationContext.Uri.ToString().Contains("AgentsView"))
    {
        // The ID of the item to be displayed is passed as a navigation parameter.
        // ToInt32 can throw FormatException or OverflowException.
        try
        {
            int id = Convert.ToInt32(navigationContext.Parameters["PersonID"]);
            if (id == 0) return;
 
            // Retrieve the specified item using the data service.
            var agent = new DAL.Agent();
            _agentsDataService.GetSelectedAgent(id, GetAgentsCallback);
         }
        catch (FormatException e)
        {
            //Console.WriteLine("Input string is not a sequence of digits.");
            //return CurrentItem == null ? false : CurrentItem.Id.Equals(id);
        }
    }
}
 
private void GetAgentsCallback(DAL.Agent agent)
{
    _navigatedToAgent = agent;
    if (_navigatedToAgent != null)
    {
        SelectedAgent = _navigatedToAgent;
    }
}
 
public DAL.Agent SelectedAgent
{
    get { return _selectedAgent; }
    set
    {
        if (_selectedAgent == value) return;
        _selectedAgent = value;
        RaisePropertyChanged(() => SelectedAgent);
        _eventAggregator.GetEvent<AgentSelectedEvent>().Publish(_selectedAgent);
    }
}

SelectedItem="{Binding SelectedAgent, Mode=TwoWay}"

5 Answers, 1 is accepted

Sort by
0
Dimitrina
Telerik team
answered on 28 Nov 2012, 08:27 AM
Hello,

We have done some changes for our most recent version - Q3 2012 - and now you should be able to set the SelectedItem before RadGridView.DataLoaded event has passed. May I ask you to confirm the version of the Telerik Controls you use?
 

Regards,
Didie
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Alan
Top achievements
Rank 1
answered on 28 Nov 2012, 01:54 PM
I am using the Trial version of RadControls for WPF Q3 2012 .
0
Dimitrina
Telerik team
answered on 30 Nov 2012, 01:48 PM
Hello,

I have created a sample project to test how the SelectedItem is set when it is initialized before the data has been loaded into the GridView. As a result the SelectedItem in the GridView was set as expected. 

May I ask you to modify my project in order to show your exact scenario? That way I could debug it locally to advise you further.

Greetings,
Didie
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Alan
Top achievements
Rank 1
answered on 30 Nov 2012, 02:34 PM
Thanks for the reply, unfortunately it would be too complicated to modify your project since I am using Prism with dependency injection.  I am now thinking that since I set the SelectedItem as the injected view is being navigated to, the RadGridView itself has not yet been initialized (loaded).  If I wanted to use the loaded event of the RadGridView to trigger setting the SelectedItem, how would I do that in an MVVM ViewModel?  Would I use a behavior to perform this?  Is the RadGridView Load event the correct event?
0
Dimitrina
Telerik team
answered on 30 Nov 2012, 03:49 PM
Hello,

Actually in my example the SelectedItem property is set before the GridView is loaded but after it has been initialized. So the proper method should be Initialized. As to your question, you could try using a behaviour. 

Kind regards,
Didie
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

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