Binding to SelectedItems

12 posts, 0 answers
  1. Frustrated Dev
    Frustrated Dev avatar
    35 posts
    Member since:
    Oct 2006

    Posted 17 Apr 2010 Link to this post

    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,
  2. Milan
    Admin
    Milan avatar
    1989 posts

    Posted 22 Apr 2010 Link to this post

    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.
  3. DevCraft banner
  4. Jacob
    Jacob avatar
    7 posts
    Member since:
    Sep 2010

    Posted 09 Feb 2011 Link to this post

    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
  5. Vlad
    Admin
    Vlad avatar
    11100 posts

    Posted 10 Feb 2011 Link to this post

    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>>
  6. Jacob
    Jacob avatar
    7 posts
    Member since:
    Sep 2010

    Posted 14 Feb 2011 Link to this post

    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
  7. Milan
    Admin
    Milan avatar
    1989 posts

    Posted 18 Feb 2011 Link to this post

    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
  8. Jacob
    Jacob avatar
    7 posts
    Member since:
    Sep 2010

    Posted 18 Feb 2011 Link to this post

    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
  9. Milan
    Admin
    Milan avatar
    1989 posts

    Posted 21 Feb 2011 Link to this post

    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
  10. Jacob
    Jacob avatar
    7 posts
    Member since:
    Sep 2010

    Posted 20 Apr 2011 Link to this post

    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
  11. Milan
    Admin
    Milan avatar
    1989 posts

    Posted 21 Apr 2011 Link to this post

    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
  12. Jacob
    Jacob avatar
    7 posts
    Member since:
    Sep 2010

    Posted 22 Apr 2011 Link to this post

    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
  13. andrian carpio
    andrian carpio avatar
    1 posts
    Member since:
    Mar 2010

    Posted 15 Dec 2011 Link to this post

    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
Back to Top
DevCraft banner