Deleting items from Code Behind with IsSynchronizedWithCurrentItem causes visual list corruption

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

    Posted 13 Nov 2013 Link to this post

    I have found a problem when using IsSynchronizedWithCurrentItem and removing items from an observable list from code-behind. Using the sample code below, if you:
    • Edit the ThirdItem Name field, then use the Delete button (while still in edit mode), ThirdItem remains on the screen but is successfully deleted from the code-behind. You can see this by sorting by any column, and the list gets redrawn correctly.
    • Edit SecondItem Name field, then use the Delete button (while still in edit mode), the list now shows one Firstitem and two ThirdItems! Again, sorting redraws the list correctly.
    There are various other strange scenarios with removing items. I haven't been able to reproduce it in the simple example, but in my application there are scenarios where more than one row are removed from the UI on delete (but fine in the ObservableCollection behind), and others where you can delete multiple rows and get one of the deleted rows to show back up (even though it doesn't exist in the Observable Collection). In all these cases, sorting again refreshes the list correctly.

    This simple example seem to work fine without the IsSynchronizedWithCurrentItem flag, but my actual application still has similar problems without this flag set when deleting the item currently being edited, so I'm not sure if it's related.

    Example Code:

    MainWindow.xaml:
    <Window x:Class="SortedChangingList.MainWindow"
            Title="MainWindow" Height="350" Width="525">
        <Grid>
            <telerik:RadGridView
                        IsSynchronizedWithCurrentItem="True"
                        AutoGenerateColumns="False"
                        ItemsSource="{Binding ItemCollection}"
                        SelectionMode="Single">
                <telerik:RadGridView.Columns>
                    <telerik:GridViewDataColumn DataMemberBinding="{Binding Path=Name}"
                                                Header="Name" />
                    <telerik:GridViewDataColumn DataMemberBinding="{Binding Path=Comment}"
                                                Header="Comment" />
                    <telerik:GridViewColumn>
                        <telerik:GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <Button
                                Command="{Binding DataContext.DeleteCommand,
                                RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type telerik:RadGridView}}}"
                                CommandParameter="{Binding }">Delete Me</Button>
                            </DataTemplate>
                        </telerik:GridViewColumn.CellTemplate>
                    </telerik:GridViewColumn>
                </telerik:RadGridView.Columns>
            </telerik:RadGridView>
        </Grid>
    </Window>

    MainWindow.xaml.cs:
    using System;
    using System.Collections.ObjectModel;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Input;
     
    namespace SortedChangingList
    {
        public class Item
        {
            public string Name { get; set; }
            public string Comment { get; set; }
        }
        public partial class MainWindow : Window
        {
            private RelayCommand _DeleteCommand;
            public ObservableCollection<Item> ItemCollection { get; set; }
            public ICommand DeleteCommand
            {
                get
                {
                    return _DeleteCommand ?? (_DeleteCommand = new RelayCommand(DeleteItem));
                }
            }
            public MainWindow()
            {
                ItemCollection = new ObservableCollection<Item>();
                ItemCollection.Add(new Item() { Name = "FirstItem" });
                ItemCollection.Add(new Item() { Name = "SecondItem" });
                ItemCollection.Add(new Item() { Name = "ThirdItem" });
     
                InitializeComponent();
     
                DataContext = this;
            }
            private void DeleteItem(object obj)
            {
                Item item = obj as Item;
                ItemCollection.Remove(item);
            }
        }
     
        public class RelayCommand : ICommand
        {
            private readonly Action<object> _Execute;
            public RelayCommand(Action<object> execute)
            {
                _Execute = execute;
            }
            public bool CanExecute(object parameter)
            {
                return true;
            }
            public event EventHandler CanExecuteChanged
            {
                add { CommandManager.RequerySuggested += value; }
                remove { CommandManager.RequerySuggested -= value; }
            }
            public void Execute(object parameter)
            {
                _Execute(parameter);
            }
        }
    }

    Louis

  2. Answer
    Vera
    Admin
    Vera avatar
    263 posts

    Posted 14 Nov 2013 Link to this post

    Hello Louis,

    Could you specify the version of RadControls for WPF you are currently using? I was able to reproduce the problem with older versions but not with our latest - Q3 2013. May I ask you to download it and to give it a try?

    Regards,
    Vera
    Telerik
    TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WPF.
    Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
    Sign up for Free application insights >>
  3. UI for WPF is Visual Studio 2017 Ready
  4. Louis
    Louis avatar
    83 posts
    Member since:
    Aug 2013

    Posted 22 Nov 2013 Link to this post

    Sorry, just noticed I didn't reply to this. The new version does indeed resolve all the related issues we were seeing. Thanks!
  5. Steven
    Steven avatar
    10 posts
    Member since:
    Mar 2011

    Posted 08 Jan 2014 Link to this post

    Hi,

    I am having the exact same issues as the original poster and have tried with both 2013.3.1016 and 2013.3.1204. The only way to fix these errors is to ensure that the row is committed prior to saving.

    I haven't noticed this in the Silverlight versions of your controls, but I believe that when the cursor (or focus) moves off the grid, the row is committed. This doesn't seem to be the case in WPF, however that would unlikely fix the error since in this application we are using commands so it is conceivable that the user would just use the related hotkey to save/delete the row.

    Is this actually a bug?

    Cheers,

    Steven

  6. Vera
    Admin
    Vera avatar
    263 posts

    Posted 08 Jan 2014 Link to this post

    Hi Steven,

    It would be better to open a support ticket and send a simple runnable project demonstrating the scenario you described.

    Regards,
    Vera
    Telerik
    TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WPF.
    Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
    Sign up for Free application insights >>
Back to Top
UI for WPF is Visual Studio 2017 Ready