I remember some folks having trouble with it when setting the dataContext at runtime rather than as a static resource. I'm using Prism's Dependency Injection to inject the viewModel into a property on the View. I can see that my ObservableCollection on my viewModel is being populated, but it's not showing up as selected items on my grid.
I need help getting this to work properly.
11 Answers, 1 is accepted
Well, it is a bit difficult to provide you with any advise with the available information. Could you please share some of your code or share a sample application that illustrates the problem?
Kind regards,
Milan
the Telerik team

<
telerik:RadGridView
ItemsSource
=
"{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=telerik:RadPane},Path=viewModel.References.TypeList}"
>
<
i:Interaction.Behaviors
>
<
this:SelectedItemsBehavior
SelectedItems="{Binding RelativeSource={RelativeSource
Mode
=
FindAncestor
,
AncestorType
=
telerik
:RadPane},
Path
=
viewModel
.ComplaintTypes}" />
</
i:Interaction.Behaviors
>
</
telerik:RadGridView
>
The code for the behavior is exactly the same as the blog post. I just renamed it to SelectedItemsBehavior.
I have an ObservableCollection property on my viewModel that is named ComplaintTypes. I have a controller that uses the UnityContainer to resolve an instance of the View...the container injects an instance of the viewModel then I retrieve the viewModel property from the view, and call an initialize method. I have a background worker fetch data from my service and populate it's public properties for binding. Once that is completed (RunWorkerCompleted), then another method is called to add items to the ObservableCollection that is bound to SelectedItems. Stepping through the code, I can see that the ObservableCollection gets the correct items, but they don't appear selected in the grid. The ItemsSource binding in the code above works.
Here is the code that populates the collection once the data has been retrieved via the service from the database.
if
(_complaint.Types.Count > 0)
{
foreach
(DataLibrary.Type itm
in
_complaint.Types)
this
._types.Add(itm);
RaisePropertyChanged(() =>
this
.ComplaintTypes);
}
I've also tried replacing the foreach loop and assigning it directly:
this
._types =
new
ObservableCollection<DataLibrary.Type>(_complaint.Types.AsQueryable());
Could you please check if ContextSelectedItems_CollectionChanged is invoked when the selected items in your ViewModel change? Then, if it is, does the Transfer method succeeds in adding all items to SelectedItems of the grid?
Best wishes,
Milan
the Telerik team

If I initialize the Collection to empty in my constructor, then add items using the collection Add method at the point I need to show selected items, I hit breakpoint at ContextSelectedItems_CollectionChanged for each item that I'm adding, then I hit breakpoint for GridSelectedItems_CollectionChanged for each item, so they all get removed again.
My collection is a public get; private set; so that I can use that property to add items, thus triggering the collection_changed event to update the UI.
Well, that is very strange - GridSelectedItems_CollectionChanged should not be hit while ContextSelectedItems_CollectionChanged is being executed.
Unfortunately I can seem to understand where the problem might be. Is it possible to send us your project so that we can get debug it on our site?
Greetings,
Milan
the Telerik team

When I click the new button, it runs the method I created that Adds one item to the SelectedItems property on the Context and it appears as selected in the grid as expected.
So maybe there is something going on with the timing of the control's initialization and binding and when the SelectedItems property on the Context is getting populated with items.
It is clear now. It could be two thing then: The first is that the selected item is synchronized with the current item which can clear a previous selection. This can happen if the property IsSynchronizedWithCurrentItem is true - could you please try setting it to false?
The second possible cause is a bug that was present in the latest service pack for Q2. This bug is not present in Q3. The ticket indicates that Q3 is being used. Is that really the case?
Kind regards,Milan
the Telerik team

That is strange... You can place a break point in SelectionChanged of the grid and see when the previous selection is cleared and the first item is selected. When you hit a break point where e.RemovedItems contains the previously selected items and e.AddedItems contains the first item observe the call stack to determine what caused the selection to be reset to the first item. Could you please paste the call stack at that point?
Kind regards,
Milan
the Telerik team

Ok, I hit breakpoints depending on where in my code, I make the changes. I'm using prism, so my code is as follows will hit breakpoints:
_view = _container.Resolve<EditorView>();
region.Add(_view);
_view.viewModel.Initialize(Number);
I can hit the breakpoints and step through the code to add all 4 items to my collection. Then I fall out of my initialize method and I step to the GridSelectedItems_CollectionChanged method on the behavior.
If I reverse the the last two lines of code above so that it calls initialize, then Add, I can't step through the code.
Just another FYI, my initialize Method, uses a background worker to fetch an entity from the dataSource. Once it completes, RunWorkerCompleted is the one that makes the call to AddTypesToCollection which loops through a collection on the entity and adds those items to the bound SelectedItems property.
I just don't know why it keeps resetting the grid once my method completes. In the sample project, If I don't set IsSynchronizedWithCurrent = True, I don't hit breakpoints. I can set it to True, hit the breakpoint and paste that callstack, if you want me to.
Providing us with come call stack might really help us resolve the problem. Could you please subscribe to SelectionChanged of the grid (separate handler from the one that exists in the Behavior) and observe when the selected it reset to the first item - at that point e.Added items should contain one item and e.AremoveItems should contain the deselected items. It would be great If you could send us the stack trace at that point.
Greetings,
Milan
the Telerik team