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

Fill RadGridView in window Loaded event

1 Answer 335 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Marat
Top achievements
Rank 1
Marat asked on 27 Dec 2016, 09:38 AM

I've found very strange behavior.

A simple project contains the window with RadGridView. ItemsSource is binded to ViewModel.Rows where Rows is ObservableCollection.

I fill rows inside Window Loaded event.

RadGridView doen't updated if I don't use Rows.Clear() before Rows.Add().

Can you explain this?

 

Here is xaml:

01.<Window x:Class="TelerikWpfApp1.MainWindow"
04.        xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
05.        xmlns:local="clr-namespace:TelerikWpfApp1"
06.        Loaded="Window_Loaded">
07.    <Window.Resources>
08.        <local:ViewModel x:Key="vm"/>
09.    </Window.Resources>
10.    <Grid DataContext="{Binding Mode=OneWay, Source={StaticResource vm}}">
11.        <telerik:RadGridView ItemsSource="{Binding Rows}" />
12.    </Grid>
13.</Window>

 

And code behind:

01.using System.Collections.ObjectModel;
02.using System.Windows;
03. 
04.namespace TelerikWpfApp1
05.{
06.    public partial class MainWindow : Window
07.    {
08.        public MainWindow()
09.        {
10.            InitializeComponent();
11.        }
12. 
13.        private void Window_Loaded(object sender, RoutedEventArgs e)
14.        {
15.            var rows = ((ViewModel)FindResource("vm")).Rows;
16.            // to make it work uncomment the next row
17.            //rows.Clear();
18.            rows.Add(new Row { Name = "Name1", Column1 = "value1_1" });
19.            rows.Add(new Row { Name = "Name2", Column1 = "value2_1" });
20.        }
21.    }
22. 
23.    class Row
24.    {
25.        public string Name { get; set; }
26.        public string Column1 { get; set; }
27.    }
28. 
29.    class ViewModel
30.    {
31.        public ObservableCollection<Row> Rows { get; } = new ObservableCollection<Row>();
32.    }
33.}

1 Answer, 1 is accepted

Sort by
0
Ivan Ivanov
Telerik team
answered on 29 Dec 2016, 04:39 PM
Hi,

Generally, when a CollectionChanged event is raised, the virtualizing panel that manages the creation a visualization of rows invokes InvalidateMeasure, so that it can respect the new changes. There are several different subscriptions to this event in RadGridView's internal logic and at the time when the items are added (Loaded event), only the Reset action events are properly handled (Clear uses Reset). In my opinion, this behavior can be treated as a minor bug. However, it seems that this scenario works fine when GroupRenderMode is set to Flat. Basically, setting this property switches RadGridView to use an alternative UI virtualziation algorithm. Since it is the recommended alternative (it is newer and better polished) I would suggest that you give it a try. Can you please confirm whether using it fits your needs?

Regards,
Ivan Ivanov
Telerik by Progress
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
Tags
GridView
Asked by
Marat
Top achievements
Rank 1
Answers by
Ivan Ivanov
Telerik team
Share this question
or