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

Binding to SelectedItems

11 Answers 273 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Frustrated Dev
Top achievements
Rank 1
Frustrated Dev asked on 17 Apr 2010, 03:13 PM
Hello,

I have a class called MyItem. This class has a property called "IsSelected". This property can get changed programmatically. How do I propogate that change back to the UI such that if this value is true, the row associated with MyItem is highlighted (selected)?

Thank you,

11 Answers, 1 is accepted

Sort by
0
Milan
Telerik team
answered on 22 Apr 2010, 11:48 AM
Hello Frustrated Dev,

One possible approach is to create e Binding using the RowLoaded event:

01.// subscribe to RowLoaded event
02.void playersGrid_RowLoaded(object sender, Telerik.Windows.Controls.GridView.RowLoadedEventArgs e)
03.{
04.    var row = e.Row as GridViewRow;
05.  
06.    if (row != null)
07.    {
08.        System.Windows.Data.Binding b = new System.Windows.Data.Binding("IsSelected");
09.        b.Source = row.Item;
10.        b.Mode = System.Windows.Data.BindingMode.TwoWay;
11.  
12.        row.SetBinding(GridViewRow.IsSelectedProperty, b);
13.    }
14.}


Regards,
Milan
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
Jacob
Top achievements
Rank 1
answered on 09 Feb 2011, 07:33 PM
I have the same problem as the original poster and I found this thread while searching for a solution. I was hoping that a better mechanism had been added to the RadGridView by now, since the RowLoaded event is called every time a row is brought into view when scrolling.

As of the 2010 Q3 release, does anybody know of a binding mechanism for GridViewRow that is more along the lines of a ContainerBinding? The programmatic selection documentation is not very useful for this case.

Thanks,
Jacob Champion
LabVIEW R&D
National Instruments
0
Vlad
Telerik team
answered on 10 Feb 2011, 08:18 AM
Hello,

I believe that the approach used in my blog post may help you in your case. 

Best wishes,
Vlad
the Telerik team
Let us know about your Windows Phone 7 application built with RadControls and we will help you promote it. Learn more>>
0
Jacob
Top achievements
Rank 1
answered on 14 Feb 2011, 11:31 PM
Vlad,

Thanks for the reply. Behaviors are something I wasn't aware of, and they look quite useful.

The approach on the blog doesn't help my case, however. Our RadGridView is bound to a collection of business objects that keep track of their IsSelected state individually. Like the original poster, I want to somehow tie the IsSelected state of the GridViewRow to the IsSelected state of the object it corresponds to. Ideally this process would be the same as using a ContainerBinding in a RadTreeView to bind the selection state. (We would like to use the same business object in both the TreeView and the GridView.)

Milan's workaround certainly works, but the bindings are regenerated every time a row is scrolled into view. Is there a less hacky way to achieve this?

Thanks again,
Jacob Champion
LabVIEW R&D
National Instruments
0
Milan
Telerik team
answered on 18 Feb 2011, 08:45 AM

Hello Jacob,

void clubsGrid_RowLoaded(object sender, Telerik.Windows.Controls.GridView.RowLoadedEventArgs e)
{
    GridViewRow row = e.Row as GridViewRow;
    if (row != null)
    {
        var binding = new Binding("MyDataIsSelectedProperty") { Mode = BindingMode.TwoWay };
  
        row.SetBinding(GridViewRow.IsSelectedProperty, binding);
    }
  
}

Well, if your data items have a property that reflects their selection state you could easily bind the this property to the IsSelected property of our GridViewRow object. You can apply this binding in our RowLoaded event. It should look something like that:



Best wishes,
Milan
the Telerik team
0
Jacob
Top achievements
Rank 1
answered on 18 Feb 2011, 04:05 PM
Milan,

Thanks for the code. I think it's the same as the post you made above, though. Isn't this code still called every time a row is scrolled into view? And will virtualized rows be bound before the user scrolls to them? I was hoping that a ContainerBinding or similar was available for the RadGridView.

Thanks,
Jacob Champion
LabVIEW R&D
National Instruments
0
Milan
Telerik team
answered on 21 Feb 2011, 09:13 AM

Hello Jacob,

Excuse me for pasting this code again. Anyway this code should be sufficient to get the work done. RowLoaded will be called every time a row is scrolled into view but that should not have any negative effect on performance. 

 The virtualized rows will not be bound until you scroll to them but that will not break your logic in any way, I guess.

Unfortunately, Silverlight does not support bindings in Styles and if it did you would have been able to do this with a simple style. 

<Style TargetType="telerik:GridViewRow">
            <Setter Property="IsSelected" Value="{Binding MyIsSelectedProperty}"/>
        </Style>



Regards,
Milan
the Telerik team
0
Jacob
Top achievements
Rank 1
answered on 20 Apr 2011, 04:39 PM
Milan,

> The virtualized rows will not be bound until you scroll to them but that will not break your logic in any way, I guess.

We implemented this workaround, and we thought so too for a while. Unfortunately, while debugging PITS issue 5621, we've now discovered that using SelectionMode.Extended for the RadGridView breaks when using this workaround -- I assume for the same reason that it breaks for the RadTreeView. Selecting items in large lists and then scrolling corrupts the grid's SelectedItems list.

Are there any other accepted solutions for binding to the IsSelected state?

Thanks,
Jacob Champion
LabVIEW R&D
National Instruments
0
Milan
Telerik team
answered on 21 Apr 2011, 08:21 AM

Hi Jacob,

Could you please elaborate on what you mean by saying that the selection breaks when using container binding?

As an alternative you could try the approach that is outlined in this blog post. It is a bit different that the one that you are currently using but you could give it a try. 



Greetings,
Milan
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
Jacob
Top achievements
Rank 1
answered on 22 Apr 2011, 07:04 PM
Milan,

I think that's the same blog Vlad linked to up above. Among other things, the performance of that Behavior can't keep up with large SelectedItems lists, but thanks for the suggestion.

As to the bug: when using SelectionMode.Extended for the RadTreeView in conjunction with your workaround, then selecting an item, scrolling it offscreen, and selecting another item does not correctly update the SelectedItems list. Scrolling back to the first item will show that it is still selected. This is not the only bug; the rest also seem to stem from an incorrect handling of selected virtualized items, similar to issue 5621.

Since I assume the same logic is being used in both places, I was hoping that there was another way to achieve the desired outcome while we wait for a fix.

Thanks again,
Jacob Champion
LabVIEW R&D
National Instruments
0
andrian carpio
Top achievements
Rank 1
answered on 16 Dec 2011, 02:23 AM
I got the same problem. The solution works after you scroll the grid. But as a workaround on this issue I pass the original collection to a temporary container in my viewmodel and clear the original and after i update the IsSelected property on temp container i bring it back to orig container this triggers the row_loaded event making the expected result. This works for me. Hope it will work in your scenario.

Cheers
Tags
GridView
Asked by
Frustrated Dev
Top achievements
Rank 1
Answers by
Milan
Telerik team
Jacob
Top achievements
Rank 1
Vlad
Telerik team
andrian carpio
Top achievements
Rank 1
Share this question
or