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

Add to SelectedItems via Behavior

4 Answers 233 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Jeremie
Top achievements
Rank 1
Jeremie asked on 04 Apr 2011, 08:55 PM
To set the scene, I am in an MVVM environment and I am using the Behavior from Vlad's blog post located at
How To: Synchronize your UI selected items with your data context using MVVM and Blend behaviors for Silverlight and WPF

This behavior works great and I have it bound to a ViewModel, I am working on extended this so when the user selects a row, the grid will automatically select any rows that have the same PO Number as the selected row.

View Model Code:
public WarehouseLineItemProxy SelectedLineItem
    {
      get { return selectedLineItem; }
      set
      {
        if (value != null)
        {
          selectedLineItem = value;
          RaisePropertyChanged(() => SelectedLineItem);
 
          //Check for any additional items that have this GP PO Number
          var lineItems = new ObservableCollection<WarehouseLineItemProxy>();
 
          foreach (var lineItem in LineItems.Where(x => x.GPPONumber == selectedLineItem.GPPONumber))
          {
            lineItems.Add(lineItem);
          }
 
          SelectedLineItems = lineItems;
        }
      }
    }

Here, the SelectedLineItem property is bound to the SelectedItem property of the RadGrid, LineItems is an ObservableCollection of business objects that serves as the ItemsSource of the RadGrid.

I then made the following changes in the Behavior:
void GridSelectedItems_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            UnsubscribeFromEvents();
 
            if (((IList)SelectedItems).Count > Grid.SelectedItems.Count)
            {
              Transfer(SelectedItems as IList, Grid.SelectedItems);
            }
            else
              Transfer(Grid.SelectedItems, SelectedItems as IList);
 
            SubscribeToEvents();
        }

So rather then transferring the Grid's selected items to the SelectedItems dependency property I reverse it and transfer the SelectedItems dependency property into the Grid's selected items.

public static void Transfer(IList source, IList target)
        {
            if (source == null || target == null)
                return;
 
            target.Clear();
 
            foreach (var o in source)
            {
                target.Add(o);
            }
        }


This works and if I look at the Grid.SelectedItems property in a watch it shows me a count of 2.  However the UI only displays one row selected.

Is there a way to force the Grid to re-draw?  Is there a better way to accomplish what I am trying to do?

Any help is greatly appreciated.

Jeremie Grund

4 Answers, 1 is accepted

Sort by
0
Maya
Telerik team
answered on 05 Apr 2011, 04:24 PM
Hello Jeremie,

Generally, the approach you follow will not be applicable as the SelectedItem does not change even when selecting new items. What you may try to do is to extend the behavior a bit as demonstrated in the sample attached. Let me know whether it corresponds to your requirements.
 

Regards,
Maya
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Jeremie
Top achievements
Rank 1
answered on 05 Apr 2011, 05:27 PM
Maya,

I downloaded and extracted the sample provided and took a look.

However when I try to call Grid.Select the intellisense doesn't report that as being an available method.  I think this may be because we are using the WPF Desktop controls rather then the Silverlight controls in your example.

Is there an example that applies to WPF?

Thanks for your help,

Jeremie
0
Maya
Telerik team
answered on 06 Apr 2011, 07:45 AM
Hi Jeremie,

Please accept my apology for the misunderstanding about the target platform. However, the approach is equal for WPF too. I am sending you the sample project for WPF implementing the exact same functionality.
As for the Select(items) method, the reason for you not to see it may be that you are using a older version of RadGridView. Which version do you use ?  

 

Greetings,
Maya
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Jeremie
Top achievements
Rank 1
answered on 06 Apr 2011, 03:01 PM
Maya,

Thanks that does exactly what I was hoping it would do.  And you are correct, the reason I do not have the Select method available is due to using an older version of the Grid. 

Thanks again!

Jeremie
Tags
GridView
Asked by
Jeremie
Top achievements
Rank 1
Answers by
Maya
Telerik team
Jeremie
Top achievements
Rank 1
Share this question
or