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

Exception when binding to ListCollectionView with filter

7 Answers 242 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Erik
Top achievements
Rank 1
Erik asked on 15 Feb 2012, 10:54 PM
If I bind a RadGridView to a ListCollectionView with a filter, and I chance a value in a Grid cell that causes the filter to return false for that row, I get an exception in the Grid cell.

Code to reproduce:

public class MyEntity : INotifyPropertyChanged
{
    private bool _myBool;
    public bool MyBool
    {
        get { return _myBool; }
        set
        {
            _myBool = value;
            PropertyChanged(this, new PropertyChangedEventArgs("MyBool"));
        }
    }
 
    public event PropertyChangedEventHandler PropertyChanged;
}

public partial class MainWindow
    {
        public MainWindow()
        {
            InitializeComponent();
            DataContext = this;
 
            MyEntities = new ObservableCollection<MyEntity> { new MyEntity(), new MyEntity(), new MyEntity() };
            MyCv = (ListCollectionView)CollectionViewSource.GetDefaultView(MyEntities);
            MyCv.Filter = MyBoolFilter;
        }
 
        public ObservableCollection<MyEntity> MyEntities { get; set; }
 
        public ListCollectionView MyCv { get; set; }
 
        private bool MyBoolFilter(object item)
        {
            return !((MyEntity)item).MyBool;
        }
    }

<Window x:Class="GridViewTest.MainWindow"
        Title="MainWindow" Height="350" Width="525">
 
    <telerik:RadGridView ItemsSource="{Binding MyCv}"/>
     
</Window>

Steps to reproduce:
1. check one of the checkboxes in the grid
2. move focus to somewhere else

I use version Q3 2011 SP1, .Net 4.0

Is there a way around this problem?


7 Answers, 1 is accepted

Sort by
0
Erik
Top achievements
Rank 1
answered on 19 Feb 2012, 02:51 PM
The issue also occurs with Q1 2012
0
Accepted
Maya
Telerik team
answered on 21 Feb 2012, 05:58 PM
Hi Erik,

Actually, this would be a kind of expected behavior. The cause of the issue is that the element which is supposed to be current is removed and the synchronization with the selected item breaks. What you can do is to set IsSynchronizedWithCurrentItem property of the grid:

<telerik:RadGridView ItemsSource="{Binding MyCv}" IsSynchronizedWithCurrentItem="False" />

Let me know whether the suggested approach corresponds to your requirements.

Regards,
Maya
the Telerik team
Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
0
Erik
Top achievements
Rank 1
answered on 21 Feb 2012, 07:19 PM
Hi Maya,

That works up-to the last item. When the grid has one row left, when I check the checkbox and place focus somewhere else, I get another exception.
0
Maya
Telerik team
answered on 22 Feb 2012, 08:57 AM
Hi Erik,

Indeed, you are quite correct. And again in this case the reason is that the item to be set as current/ selected is no more available. What I could suggest in this case is to verify whether the item to which a filter is applied is null or not:

private bool MyBoolFilter(object item)
       {
           if (item != null)
           {
               return !((MyEntity)item).MyBool;
           }
           else
               return false;
       }

Nevertheless, we will investigate the issue and check whether we can update the source so that no such exception is caused anyway.
Thank you for your cooperation. I have updated your telerik points accordingly. 

Regards,
Maya
the Telerik team
Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
0
Erik
Top achievements
Rank 1
answered on 22 Feb 2012, 11:31 AM
Hi Maya,

The MyBoolFilter is one thing I checked, object item is never null there
0
Maya
Telerik team
answered on 22 Feb 2012, 12:33 PM
Hello Erik,

Would you take a look at the sample attached ?  Can you verify whether you can  get the same exception on it ? 

Kind regards,
Maya
the Telerik team
Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
0
Erik
Top achievements
Rank 1
answered on 22 Feb 2012, 03:57 PM
Hi Maya,

Turns out that I was mistaken and you are correct, your modification of the MyBoolFilter function fixes the problem. I was testing with a different project where the exception somehow turned up somewhere else completely.

IsSynchronizedWithCurrentItem="False" solves my initial issue, the other issue was my mistake
Tags
GridView
Asked by
Erik
Top achievements
Rank 1
Answers by
Erik
Top achievements
Rank 1
Maya
Telerik team
Share this question
or