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

IsSelected and EnableRowVirtualization

1 Answer 923 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Louis
Top achievements
Rank 1
Louis asked on 07 Jul 2014, 09:05 PM

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"
        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;
}

1 Answer, 1 is accepted

Sort by
0
Yoan
Telerik team
answered on 09 Jul 2014, 01:33 PM
Hello,

The reason for this behaviour is that RadGridView is virtualized control and as such, it only realizes the visual elements that are visible. You can check the UI Virtualization help article for a reference. 

You can avoid this by disabling the virtualization, but this will slow down the performance as all the items should be initially loaded. However, I would recommend you to check this github example for binding to the SelectedItems property of the GridView directly.

As a side note - although GitHub is a very well-known platform we saw a better and easier approach for reviewing our examples developing our brand new SDK Samples Browser. You can also use it to review the examples.

Regards,
Yoan
Telerik
 
Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
 
Tags
GridView
Asked by
Louis
Top achievements
Rank 1
Answers by
Yoan
Telerik team
Share this question
or