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:
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:
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.
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
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