RadDataBoundListBox not updated when ItemSource changes or replaced

2 posts, 0 answers
  1. Stefan
    Stefan avatar
    21 posts
    Member since:
    Oct 2013

    Posted 06 Dec 2013 Link to this post

    Hi,

    I have a problem using the the RadDataBoundListBox. Its ItemSource is set to an ObservableCollection but the ListBox is not updated automatically when the Collection is changed or completely replaced:

    <DataTemplate x:Key="GridItemTemplate">
        <Border Background="#365F70" Width="128" Height="128" Margin="24,0,0,24">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="80"/>
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>
     
                <ContentControl Template="{Binding Template}" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="-15,0,0,0">
                    <ContentControl.RenderTransform>
                        <CompositeTransform ScaleX="1.5" ScaleY="1.5"/>
                    </ContentControl.RenderTransform>
                </ContentControl>
                <TextBlock Text="{Binding Name}" FontSize="20" Grid.Row="1" VerticalAlignment="Center" HorizontalAlignment="Center"/>
            </Grid>
        </Border>
    </DataTemplate>
     
    <DataTemplate x:Key="ListItemTemplate">
        <Border Background="#365F70" Height="72" Margin="24,0,24,12">
            <StackPanel Orientation="Horizontal">
                <ContentControl Margin="10,-15,35,0" Template="{Binding Template}" VerticalAlignment="Center">
                    <ContentControl.RenderTransform>
                        <CompositeTransform ScaleX="1.5" ScaleY="1.5"/>
                    </ContentControl.RenderTransform>
                </ContentControl>
                <TextBlock Text="{Binding Name}" FontSize="20" VerticalAlignment="Center"/>
            </StackPanel>
        </Border>
    </DataTemplate>

    <telerikPrimitives:RadDataBoundListBox
                            x:Name="RadListBox"
                            ItemTemplate="{StaticResource GridItemTemplate}"
                            ItemTap="RadListBox_OnItemTap"
                            ItemReorderStateChanged="RadListBox_ItemReorderStateChanged"/>

    ObservableCollection<Data> dataList;
            private void UpdateDataList() {
                if (dataList == null) {
                    dataList= new ObservableCollection<Data>();
                    RadListBox.ItemsSource = dataList;
                } else {
                    dataList.Clear();
                }
     
                for (int i = 0; i < DataSource.Count; i++) {
                    Data data = DataSource[i];
                    dataList.Add(data);
                }
                 
                RadListBox.UpdateLayout();
            }
    private void ToggleClick(object sender, EventArgs e) {
        if (listShown) {
            RadListBox.ItemTemplate = (DataTemplate)Resources["GridItemTemplate"];
            RadListBox.VirtualizationStrategyDefinition = new WrapVirtualizationStrategyDefinition { Orientation = System.Windows.Controls.Orientation.Horizontal };
            RadListBox.IsItemReorderEnabled = false;
        } else {
            RadListBox.ItemTemplate = (DataTemplate)Resources["ListItemTemplate"];
            RadListBox.VirtualizationStrategyDefinition = new StackVirtualizationStrategyDefinition { Orientation = System.Windows.Controls.Orientation.Vertical };
            RadListBox.IsItemReorderEnabled = true;
        }
    }

    Whenever the UpdateList() Methode is called nothing happens - new items are not shown, deleted items are still visible, etc. Only when using the Toogle function to switch between Grid and List Layout the ListBox is Updated and all Items are shown correctly.

    Setting RadListBox.ItemSource to a complete new instance of ObservableCollection instead of Cleaning and re-filling the existing one does not work either.

    UPDATE:
    I have to correct myselfe. The Update works properly when the List Layout is used but not in Grid mode.
  2. Stefan
    Stefan avatar
    21 posts
    Member since:
    Oct 2013

    Posted 07 Dec 2013 Link to this post

    OK, I found the source of the problem. When the datalist is refreshed it is filled up with the same objects as before. If these objects do not implement notification on property changes the listBox does not update its view, no matter if the objects are re-inserted or not. Implementing INotifyPropertyChanged on these objects solved the problem.

    I was aware that the list needs notification of property changes to update automaticially but I thought the list would update anyway if the objects are removed and re-inserted.
  3. DevCraft banner
Back to Top