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

Dynamically Load Carousel and set ItemSource at Runtime

8 Answers 217 Views
Carousel
This is a migrated thread and some comments may be shown as answers.
Tony
Top achievements
Rank 1
Tony asked on 20 Feb 2015, 05:27 PM
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

8 Answers, 1 is accepted

Sort by
0
Maya
Telerik team
answered on 24 Feb 2015, 09:34 AM
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.

 
0
Tony
Top achievements
Rank 1
answered on 24 Feb 2015, 10:10 AM
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
0
Maya
Telerik team
answered on 24 Feb 2015, 11:41 AM
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.

 
0
Tony
Top achievements
Rank 1
answered on 24 Feb 2015, 08:06 PM
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
0
Maya
Telerik team
answered on 25 Feb 2015, 08:21 AM
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.

 
0
Tony
Top achievements
Rank 1
answered on 25 Feb 2015, 04:51 PM
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
0
Maya
Telerik team
answered on 26 Feb 2015, 08:01 AM
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.

 
0
Tony
Top achievements
Rank 1
answered on 26 Feb 2015, 05:32 PM
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
Tags
Carousel
Asked by
Tony
Top achievements
Rank 1
Answers by
Maya
Telerik team
Tony
Top achievements
Rank 1
Share this question
or