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

Binding SelectedItems with Behavior

11 Answers 770 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Rayne
Top achievements
Rank 1
Rayne asked on 06 Dec 2010, 08:50 PM
I'm trying to bind RadGridView SelectedItems property to a property on my ViewModel using the custom Behavior approach mentioned in the blog post, but it's not working.

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

Sort by
0
Milan
Telerik team
answered on 07 Dec 2010, 02:19 PM
Hi Rayne,

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
Browse the videos here>> to help you get started with RadControls for WPF
0
Rayne
Top achievements
Rank 1
answered on 07 Dec 2010, 03:19 PM
Yeah. Sorry about that. Here is the XAML:

<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());
0
Milan
Telerik team
answered on 08 Dec 2010, 05:03 PM
Hello Rayne,

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
Browse the videos here>> to help you get started with RadControls for WPF
0
Rayne
Top achievements
Rank 1
answered on 08 Dec 2010, 05:32 PM
A bit of further investigation revealed some interesting info.

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.
0
Milan
Telerik team
answered on 10 Dec 2010, 10:38 AM
Hi Rayne,

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
Browse the videos here>> to help you get started with RadControls for WPF
0
Rayne
Top achievements
Rank 1
answered on 10 Dec 2010, 02:44 PM
OK, I have found a bit more information. Modifying the sample project, I added a button that calls a method on the datacontext to add items to the SelectedItems property. This works as expected, but stepping through, I think what is happening is that the grid automatically selects the first item in the grid by default when it's initialized, so it runs through the GridSelectedItems_CollectionChanged to add that one item, then (I'm not sure about this one), it runs the method again and deselects the first item in the Grid, therefore modifying the bound property.

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.
0
Milan
Telerik team
answered on 10 Dec 2010, 04:12 PM
Hi Rayne,

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
Browse the videos here>> to help you get started with RadControls for WPF
0
Rayne
Top achievements
Rank 1
answered on 10 Dec 2010, 04:34 PM
I double checked the version. The sample was using Q1 controls. So I upgraded them to Q3. Now when I run the project, the ListBox that has it's ItemsSource bound to the SelectedItems property shows the one item that I added in the constructor of the DataContext. But the grid doesn't show that item selected. Setting breakpoints on both CollectionChanged methods in the behavior, I'm not hitting either breakpoint, which shows that the Property is now not being synchronized to the grid. This is the case regardless of whether I set IsSynchronizedWithCurrentItem to False or leave it out completely.

0
Milan
Telerik team
answered on 10 Dec 2010, 04:49 PM
Hi Rayne,

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
Browse the videos here>> to help you get started with RadControls for WPF
0
Rayne
Top achievements
Rank 1
answered on 10 Dec 2010, 06:21 PM

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.
0
Milan
Telerik team
answered on 16 Dec 2010, 04:07 PM
Hello Rayne,

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
Browse the videos here>> to help you get started with RadControls for WPF
Tags
GridView
Asked by
Rayne
Top achievements
Rank 1
Answers by
Milan
Telerik team
Rayne
Top achievements
Rank 1
Share this question
or