Dynamically Load Carousel and set ItemSource at Runtime

9 posts, 0 answers
  1. Tony
    Tony avatar
    24 posts
    Member since:
    Sep 2014

    Posted 20 Feb 2015 Link to this post

    Hi,

    I want to know if it's possible to dynamically load the radCarousel and set it's itemsource in C# at runtime?

    Basically I have a winform application I am integrating a WPF carousel into. The user will use a drop down selection box to choose which set of data to represent. That selected item will send a variable to the carousel, C# logic will dynamically query a SQL Server database and present the returned data in the carousel as selectable items.

    That's the goal anyway. Thanks in advance for your help!

    Tony
  2. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 24 Feb 2015 Link to this post

    Hi Tony,

    Generally, loading RadCarousel and setting its ItemsSource should be the same as loading any ItemsControl or Control as a whole. You just need to create it: var carousel = new RadCarousel(); and set its ItemsSource: carousel.ItemsSource = myCollection. 
    Could you try loading regular ItemsControl and verify whether you have problems with that ? Does creating a RadCarousel behave differently ? 

    Regards,
    Maya
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  3. UI for WPF is Visual Studio 2017 Ready
  4. Tony
    Tony avatar
    24 posts
    Member since:
    Sep 2014

    Posted 24 Feb 2015 in reply to Maya Link to this post

    Hi,

    I'm attaching the carousel to a database and the carousel loads just fine without problem. The reason I need to load it dynamically is because it will be displaying a varying set of data depending on user selection.

    I will try the method approach you suggest.

    Thanks,

    Tony
  5. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 24 Feb 2015 Link to this post

    Hello Tony,

    In this case, why no updating only the ItemsSource of the carousel ? There will be no need to load the control itself, but just changing the source collection. 

    Regards,
    Maya
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  6. Tony
    Tony avatar
    24 posts
    Member since:
    Sep 2014

    Posted 24 Feb 2015 in reply to Maya Link to this post

    Maya you are correct. I actually embarked on that method instead of loading the control. However I'm not encountering the following obstacle.

    Upon each successive load of a new collection of data, I am clicking a respective carousel panel item and retrieving the value I'm seeking however when I go to change the ItemSource for the carousel to a new collection I am getting the following error:


    An exception of type 'System.NullReferenceException' occurred...but was not handled in user code

    Additional information: Object reference not set to an instance of an object.

    Check to determine if the object is null before calling the method.

    Use the "new" keyword to create an object instance


    This error or occurs after I click on any panel. With no mouse click on a panel I am able to successfully change the ItemSource without incident.

    My guess is that the carousel panel item is retaining the focus so that when I attempt to change it's data source it throws the error. I'm not sure if there is a method or property that could aid me in removing focus? I've been going thru the list to see what might help but remain so far unsuccessful.

    Thanks for your expertise and time, helps tremendously.


    Here is the code where I'm retrieving the value of the particular datarow

    private void MyCarousel_SelectionChanged(object sender, Telerik.Windows.Controls.SelectionChangeEventArgs e)
            {
                
                object Obj = this.MyCarousel.SelectedItem;
                DataRow Row = ((DataRowView)Obj).Row;
                txtBox.Text = Row["COLID"].ToString();
            }


    Hopefully that makes sense.


    Tony
  7. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 25 Feb 2015 Link to this post

    Hello Tony,

    Will it be possible to paste the StackTrace of the exception ? It might be something in the control that I can fix right away.


    Regards,
    Maya
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  8. Tony
    Tony avatar
    24 posts
    Member since:
    Sep 2014

    Posted 25 Feb 2015 in reply to Maya Link to this post

    The only stack trace I can get is referencing the code segment I mentioned yesterday :(


    StackTrace:
           at MBM.ChartCtrl.MyCarousel_SelectionChanged(Object sender, SelectionChangeEventArgs e) in c:\_Repositories\Trunk\MBM\ChartCtrl.xaml.cs:line 99
  9. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 26 Feb 2015 Link to this post

    Hi Tony,

    In this case, some of the elements you are working with in the SelectionChanged handler are null. Could you verify which one and probably set != null check ? Does it work correctly then ? 

    Regards,
    Maya
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  10. Tony
    Tony avatar
    24 posts
    Member since:
    Sep 2014

    Posted 26 Feb 2015 in reply to Maya Link to this post

    Hi Maya,

    I've encased the code in a try...catch trapping the error which does allow the operation to continue. Not ideal but it works. I'd still like to figure out the issue when time allows. But for now this matter is settled.

    Thanks again,

    Tony
Back to Top
UI for WPF is Visual Studio 2017 Ready