IsSelected and EnableRowVirtualization

2 posts, 0 answers
  1. Louis
    Louis avatar
    83 posts
    Member since:
    Aug 2013

    Posted 07 Jul 2014 Link to this post

    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;
    }
  2. Yoan
    Admin
    Yoan avatar
    1066 posts

    Posted 09 Jul 2014 Link to this post

    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.
     
  3. UI for WPF is Visual Studio 2017 Ready
Back to Top