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

updating the multiSelectBehvior form the view model

3 Answers 89 Views
GridView
This is a migrated thread and some comments may be shown as answers.
ken
Top achievements
Rank 1
ken asked on 07 Jul 2011, 07:49 PM
Hi there.

So if you reference this post, http://www.telerik.com/community/forums/wpf/gridview/add-to-selecteditems-via-behavior.aspx you will see that Maya gave us some sample code with the custom behavior that lets us manipulate the selected items of a gridview from the code behind, i.e. changing the selected items list shows up on the grid as selected. the only problem is that we are changing the list of items that the SelectedItems property is bound to inside of the view model and not on the code behind for the page so we can't say something like
//This is from the window.xaml.cs file
((MyDataContext)
this.LayoutRoot.DataContext).SelectedItems.Add(((MyDataContext)this.LayoutRoot.DataContext).Data.Where(x => x.ID == 4).First());
Any ideas on how we can make this work? I tried something like this in the view model
//This is from the view model
public
LineItem SelectedItem
{
  get
  {
    return selectedItem;
  }
  set
  {
    selectedItem = value;
    selectedItems.Clear();
    var list = Data.Where(x => x.OrderId == value.OrderId);
    foreach (var lineItem in list)
    {
      SelecedItems.Add(lineItem);
    }
    OnPropertyChanged("SelectedItem");
  }
}
but that doesn't work. There is something I am missing here, some sort of notification is taking place when you say - MyRadGridView.SelecedItems.Add() that doesn't take place from the view model even thought they are the same collection of objects. If I could figure out what that is maybe I could figure out how to make this thing work.

Thanks for any help or suggestions...

3 Answers, 1 is accepted

Sort by
0
Milan
Telerik team
answered on 08 Jul 2011, 06:47 AM
Hi Ken,

Have you seen the blog post that Maya has mentioned? It demonstrates how you can manipulate the selected items from your ViewModel.

Greetings,
Milan
the Telerik team

Register for the Q2 2011 What's New Webinar Week. Mark your calendar for the week starting July 18th and book your seat for a walk through of all the exciting stuff we will ship with the new release!

0
ken
Top achievements
Rank 1
answered on 11 Jul 2011, 04:54 PM
So check it out - if you look at the link I put in the top of the post and the sample on the blog post you linked too it's the same exact thing (different posts / questions, same sample code). While the sample project is really nice it doesn't quit work for my situation because updating the Selected items from the view model doesn't cause the grids attached behavior to run the CollectionChanged event.

I believe it has something to do with the "owner" of the attached behavior? (don't know if I am even articulating the concept correctly). It can be a bit confusing cause the viewModel has a "SelectedItems" and the code for the multi select behavior also has a "SelectedItems" List (or something that inherits from INotifyCollectionChanged). They are of course synchronized through databinding however - in the viewmodel when I add to the selected items list it doesn't cause (as stated previously) the collection changed event for the selected items in the behavior... See what I mean? I realize this is a bit confusing cause of the two selected items but if you can grook what I am talking about then I think you might have the skills to suggest an possible solution, this one is at the bleeding edge of my mvvm/wpf ability's.

Thanks!
0
ken
Top achievements
Rank 1
answered on 14 Jul 2011, 02:27 PM
Hi, is there any movement on this? This is something we really need to accomplish. I know this is a little confusing however I think I did pretty well in describing what is happening and what we need to do, any suggestions would be great!!!

Thanks
Tags
GridView
Asked by
ken
Top achievements
Rank 1
Answers by
Milan
Telerik team
ken
Top achievements
Rank 1
Share this question
or