I'm having a problem where the IsSelected property of a row item is not getting set correctly if that row is not visible with EnableRowVirtualization="True". Only the rows that are visible get their IsSelected updated correctly. This is a problem when you want to perform an action on all selected items. See below for a simple example to illustrate. If you:
- Run it with EnableRowVirtualization="False"
- Select the first item
- Scroll to the bottom
- Select the last item
then the Selected Rows is 100, which is correct. Now if you repeat the test with EnableRowVirtualization="True", you see you only have 12 rows selected. Now scroll up, and more and more rows will be selected as they come into view.
Is there a way to either force the IsSelected property to be updated on non-visible rows, or a way to retrieve the actual list of selected items?
Thanks,
Louis
XAML:
<
Window
x:Class
=
"VirtualIsSelected.MainWindow"
xmlns:telerik
=
"http://schemas.telerik.com/2008/xaml/presentation"
Title
=
"MainWindow"
Height
=
"350"
Width
=
"525"
>
<
Grid
>
<
Grid.RowDefinitions
>
<
RowDefinition
Height
=
"*"
/>
<
RowDefinition
Height
=
"Auto"
/>
</
Grid.RowDefinitions
>
<
telerik:RadGridView
Grid.Row
=
"0"
x:Name
=
"radGridView"
AutoGenerateColumns
=
"True"
ItemsSource
=
"{Binding Path=Segments}"
SelectionMode
=
"Extended"
GroupRenderMode
=
"Flat"
EnableRowVirtualization
=
"True"
>
<
telerik:RadGridView.Resources
>
<
Style
TargetType
=
"{x:Type telerik:GridViewRow}"
>
<
Setter
Property
=
"IsSelected"
Value="{Binding
Path
=
IsSelected
,
Mode
=
OneWayToSource
}"/>
</
Style
>
</
telerik:RadGridView.Resources
>
</
telerik:RadGridView
>
<
StackPanel
Grid.Row
=
"1"
Orientation
=
"Horizontal"
>
<
Label
>Selected Rows:</
Label
>
<
TextBox
Text
=
"{Binding SelectedCount}"
IsReadOnly
=
"True"
/>
</
StackPanel
>
</
Grid
>
</
Window
>
Code-Behind:
public
class
Segment
{
private
bool
_IsSelected =
false
;
private
MainWindow _MyWindow;
public
Segment(MainWindow window)
{
_MyWindow = window;
}
public
int
Value {
get
;
set
; }
public
bool
IsSelected
{
get
{
return
_IsSelected; }
set
{
_IsSelected = value;
_MyWindow.RefreshCount();
}
}
}
public
partial
class
MainWindow : Window, INotifyPropertyChanged
{
public
int
SelectedCount {
get
;
set
; }
public
Collection<Segment> Segments {
get
;
set
; }
public
MainWindow()
{
Segments =
new
Collection<Segment>();
for
(
int
x = 0; x < 100; x++)
{
Segments.Add(
new
Segment(
this
) { Value = x });
}
SelectedCount = 0;
InitializeComponent();
DataContext =
this
;
}
public
void
RefreshCount()
{
SelectedCount = Segments.Where(s => s.IsSelected).Count();
if
(PropertyChanged !=
null
)
PropertyChanged(
this
,
new
PropertyChangedEventArgs(
"SelectedCount"
));
}
public
event
PropertyChangedEventHandler PropertyChanged;
}