Clear ItemsSource raises OutOfRangeException

11 posts, 0 answers
  1. Christophe
    Christophe avatar
    16 posts
    Member since:
    May 2014

    Posted 13 Jun 2014 Link to this post

    Hello,

    So here's my problem : I have 2 GridViewComboBoxColumn, let's call them cb1 and cb2, filled by default. I want that if I select a value in cb1, values in cb2 change ; if I select another value in cb1, values in cb2 change again.

    To do so, I wanted to .Clear() my ObservableCollection<String> which is bound to my cb2, and then to .Add(new String()).

    The problem is that an OutOfRangeException occurred on the .Clear() function. I saw somewhere that it was because I can't change anything on a property which is bound to the SelectedValueMemberPath property of the comboBox ; that's why I removed this row in my code. Now it works, I can empty and fill my ObservableCollection, but I can't get the selectedValue in cb2 anymore.

     Here's a sample of my problem, though I couldn't do anything for the dataSet which is filled by an Sql query in my code. Instead, I kept only one column inside, "reference", which represents the reference of the cameras used in my grid, in cb1. Values in cb2 are set in the Resources class.

    SampleProject

    Thanks for your help !
  2. Dimitrina
    Admin
    Dimitrina avatar
    3769 posts

    Posted 17 Jun 2014 Link to this post

    Hi,

    When I start the project, it has no rows and ad I press the "Click here to add new item" button, I get empty ComboBoxes and the following BindingExpression errors:
    BindingExpression:Path=reference; DataItem='Object' (HashCode=36038956); target element is 'LookupElement' (Name=''); target property is 'SelectedValue' (type 'Object')
    System.Windows.Data Error: 40 : BindingExpression path error: 'reference' property not found on 'object' ''Object' (HashCode=36038956)'. BindingExpression:Path=reference; DataItem='Object' (HashCode=36038956); target element is 'ValueSetter' (Name=''); target property is 'Value' (type 'Object').

    Then, I commented the code of the first ComboBox column and I only tested with the second one.
    It seems to be configured wrong, as I cannot see the values as I start editing that column. The ComboBox Column article explains how you should properly configure it.
    I can also suggest you to check the GridView / ComboboxColumn examples through the SDK Samples Browser

    After this, would you please share what steps I need to perform so that I can observe the problem you meet?

    Regards,
    Didie
    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. Christophe
    Christophe avatar
    16 posts
    Member since:
    May 2014

    Posted 17 Jun 2014 Link to this post

    Hello,

    Yes, this is normal, that's why I said before that I couldn't fill my dataSet since I do it with an Sql query in my code.I gave you another source, an ObservableCollection of class Camera. Sorry I didn't have the time to test it out before, but it seems to work now.

    So I gave you my solution : now, I can .Clear() my ObservableCollection in order to refill it with the .Add() function. It works, but as I said it works only because I've deleted the "SelectedValueMemberPath="ListFps"" property in my Xaml code, else it raises the OutOfRangeException. Without this property, I can't keep the selectedValue in my Grid as soon as I click somewhere else. I wish I could do both of these steps.

    SampleProject

    Thanks !
  4. Dimitrina
    Admin
    Dimitrina avatar
    3769 posts

    Posted 19 Jun 2014 Link to this post

    Hi Christophe,

    I tested the updated sample. 
    I run it and the values in the Fps column are available.
    Then, I clicked on the "Click here to add new item" button and I got a Null Reference Exception on line:
    result = (e.OriginalSource as RadComboBox).SelectedValue.ToString().Split(new string[] { " - " }, StringSplitOptions.RemoveEmptyEntries); 
    The reason is that there is currently not a SelectedValue.

    I then modified your code like so:
    var selectedValue = (e.OriginalSource as RadComboBox).SelectedValue;
    if (selectedValue != null)
    {
        result = selectedValue.ToString().Split(new string[] { " - " }, StringSplitOptions.RemoveEmptyEntries);
        resources.ListFps.Clear();
        ...
    }

    I found the call ListFps.Clear() only in OnSelectionChanged method and I was able to reproduce the issue you reported. It is indeed a very strange exception. As you mentioned I only get it if a SelectedValueMemberPath is specified. The Value you have specified for it is actually wrong. The Fps object does not have a Fps property, it has only NbFps property.
    Therefore, to be correct you should define it like so:
    SelectedValueMemberPath="NbFps".
     
    I tested it that way and it worked fine. The ComboBox Column also article explains how you should properly configure it.

    Regards,
    Didie
    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.
     
  5. Christophe
    Christophe avatar
    16 posts
    Member since:
    May 2014

    Posted 19 Jun 2014 in reply to Dimitrina Link to this post

    Hello,

    You're right, I was wrong in my SelectedValueMemberPath property and it works for me now, thank you.

    Still, the value I select disappears when I click somewhere else, did you get this problem too ?

    Thanks for your link but I've seen it the first time, and I read it even before, many times. Even if it's useful on some points, I don't find it well explained, since it misses some piece of code behind I have to guess. It may be useful for many users as they already know this code behind part, but I just started to learn WPF few months ago, so I don't find any answer by reading this.

    Thanks !
  6. Dimitrina
    Admin
    Dimitrina avatar
    3769 posts

    Posted 19 Jun 2014 Link to this post

    Hello,

    You can find runnable solutions from our online SDK repository here, the examples are listed under GridView.
      
    Although GitHub is a very well-known platform we saw a better and easier approach for reviewing our examples we have developed our brand new SDK Samples Browser. The SDK Samples Browser is an application that automatically connects to GitHub and downloads the XAML SDK repository locally to a location that could be set by the user. After download is complete the application gives a very convenient navigation between all the available examples and a search functionality which doesn't simply search by control’s names but also through the example’s names and descriptions.

    I hope this helps to better understand the explanation from the article.

    Regards,
    Didie
    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.
     
  7. Christophe
    Christophe avatar
    16 posts
    Member since:
    May 2014

    Posted 19 Jun 2014 in reply to Dimitrina Link to this post

    Hello,

    Ok thanks again, I already used your examples in your demo, it helped me better.

    But, I still have my problem... I can't keep the value I select displayed in my grid.

    Thanks
  8. Dimitrina
    Admin
    Dimitrina avatar
    3769 posts

    Posted 19 Jun 2014 Link to this post

    Hello,

    You should have a valid DataMemberBinding defined for GridViewComboBoxColumn to correspond to the specified SelectedValueMemberPath. You will need to extend your Camera object with an additional property corresponding to NbFps and use it as DataMemberBinding.

    Regards,
    Didie
    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.
     
  9. Christophe
    Christophe avatar
    16 posts
    Member since:
    May 2014

    Posted 20 Jun 2014 in reply to Dimitrina Link to this post

    Hello,

    I think I didn't get what you said. Here's my code where I've added the DataMemberBinding row :
    <telerik:GridViewComboBoxColumn Header="Fps"
    UniqueName="Fps"
    DisplayMemberPath="NbFps"
    EditTriggers="CellClick"
    DataMemberBinding="{Binding NbFps}"
    SelectedValueMemberPath="NbFps">

    Not sure I understood well, what do you mean exactly by "You will need to extend your Camera object with an additional property corresponding to NbFps and use it as DataMemberBinding" ? I already have a property named NbFps which is a getter / setter to the nbFps string.

    Thanks
  10. Dimitrina
    Admin
    Dimitrina avatar
    3769 posts

    Posted 20 Jun 2014 Link to this post

    Hi,

    In the last sample project you sent me, the Camera object does not have a NbFps proeprty.
    It is defined like so:
    public class Camera : INotifyPropertyChanged
    {
        private string name;
     
        public event PropertyChangedEventHandler PropertyChanged;
     
        private void EventPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
     
        public string Name
        {
            get
            {
                return this.name;
            }
            set
            {
                this.name = value;
                this.EventPropertyChanged("Name");
            }
        }
     
        public Camera()
        {
     
        }
    }
     
    You need to define a new such property for this class.

    Regards,
    Didie
    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.
     
  11. Christophe
    Christophe avatar
    16 posts
    Member since:
    May 2014

    Posted 20 Jun 2014 in reply to Dimitrina Link to this post

    Hello,

    Ha yes, ok, I already had this property in my Fps class, I didn't see the point to duplicate it in the Camera class but it works, I think I can find a way to use only one but I'll look for it later. Not sure why it works since the ItemsSource of this comboBox is set to my Fps class through ListFps<Fps>, but it works.

    I did the same thing to my camera comboBoxColumn by adding <dataMemberBinding="{Binding Name}">. It raised an exception in my OnSelectionChanged event because CurrentColumn was null, so I added a condition to check if it was null or not before everything else. Not sure if it's normal but it works, I can search something for that later too.

    Though, when I had <dataMemberBinding="{Binding ListCameras}"> it had the advantage that it only displayed one row at the beginning, with my cameras inside the comboBox. Now, there's one row per camera, it looks a bit weird since my grid is not supposed to act because of the cameras I have in my list.

    Binding is a bit tricky when you start with it...

    Thanks
Back to Top