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
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.
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
I believe that the approach used in my blog post may help you in your case.
Best wishes,Vlad
the Telerik team
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
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
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
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
> 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
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
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
Cheers