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

Why is getting the SelectedItem so Hard?

6 Answers 240 Views
Carousel
This is a migrated thread and some comments may be shown as answers.
Stephan
Top achievements
Rank 1
Stephan asked on 16 Jan 2013, 06:42 PM
I have been going through the Forum and Google tring to find how I get the current Item of the Carousel control.  I need to get one value so that I can proceed to getting my data.  When I pause the program I can see all the item in CurrentItems, but when I try to code CurrentItem.fname that is not an option.  It appear as though this control has changed, but there is still information for the old way along with work arounds that no longer work.  Could you please Help make this Work?

My XAML for just the Carousel is
<telerik:RadCarousel x:Name="RadCarousel"
                                     Background="Transparent"
                                     Margin="10,10,0,0"
                                     Height="72"
                                     VerticalAlignment="Top"
                                     HorizontalScrollBarVisibility="Hidden"
                                     ItemTemplate="{StaticResource CarouselItemTemplate}"
                                     ItemsSource="{Binding}" SelectionChanged="RadCarousel_SelectionChanged"
                                     SelectedItem="{Binding SelectedItem, Mode=TwoWay}">
                    <telerik:RadCarousel.ItemsPanel>
                        <ItemsPanelTemplate>
                            <telerik:RadCarouselPanel Path="{StaticResource horizontalPath}"/>
                        </ItemsPanelTemplate>
                    </telerik:RadCarousel.ItemsPanel>
                </telerik:RadCarousel>

 

 

 




my Code behind is this

public void LoadSalesPerson(object sender, RoutedEventArgs e)
        {
            DataClasses1DataContext conn = new DataClasses1DataContext();
            List<GetSalesPeopleResult> getSalesPeopleResults = (from s in conn.GetSalesPeople()
                                                                select s).ToList();
 
            RadCarousel.ItemsSource = getSalesPeopleResults;
        }
 
        private void RadCarousel_SelectionChanged(object sender, Telerik.Windows.Controls.SelectionChangeEventArgs e)
        {
 
            int index = RadCarousel.Items.IndexOf(RadCarousel.SelectedItem);
            int smm = RadCarousel.ElementAt(index);
             
        }

 

6 Answers, 1 is accepted

Sort by
0
Yoan
Telerik team
answered on 17 Jan 2013, 10:09 AM
Hi Stephan,

You can handle SelectionChanged event of RadCarousel and find the current item (it would be CurrentItem property of RadCarousel). You can cast this item to the required type. For example, if your business object is Employee, you can do as follows:

var currentEmployee = this.MyCarousel.CurrentItem as Employee;
 and the get the value of the property you want - currentEmployee.FirstName;


I hope this helps.

Kind regards,
Yoan
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Stephan
Top achievements
Rank 1
answered on 17 Jan 2013, 08:50 PM
Yes, I figured it out last night.  I could have kicked myself.
0
Tony
Top achievements
Rank 1
answered on 17 Feb 2015, 05:45 AM
This is my problem as well but not working from a  business object but instead using the example you've provided at your site.


public MainWindow()
{
InitializeComponent();

List<Person> persons = new List<Person>();
persons.Add(new Person() { Name = "Peter", Years = 25 });
persons.Add(new Person() { Name = "George", Years = 40 });
persons.Add(new Person() { Name = "Maria", Years = 21 });
persons.Add(new Person() { Name = "Michael", Years = 60 });
persons.Add(new Person() { Name = "Samuel", Years = 15 });

this.myRadCarousel.ItemsSource = persons;
}


How would I get the particular age of any carousel item clicked?

Thanks.














0
Stefan
Telerik team
answered on 19 Feb 2015, 01:31 PM
Hello Tony,

A possible way to achieve your goal is to follow these steps:

1.   Set a name of your RadCarousel:
<telerik:RadCarousel x:Name="myRadCarousel"/>

2.   Subscribe to the SelectionChanged event of RadCarousel:
this.myRadCarousel.SelectionChanged += myRadCarousel_SelectionChanged;

3.   Within the event handler you could implement logic similar to the proposed below:
void myRadCarousel_SelectionChanged(object sender, SelectionChangeEventArgs e)
{
    var carousel = e.OriginalSource as RadCarousel;
 
    var selected = carousel.SelectedItem as Person;
 
    if (selected != null)
    {
        int capacity = selected.Age;
    }
}

I hope that this helps.

Best Regards,
Stefan
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 19 Feb 2015, 04:49 PM
Thanks for the suggestion. I'll give it try and let you know how it goes.

Thanks.

Tony
0
Stefan
Telerik team
answered on 24 Feb 2015, 03:55 PM
Hello Tony,

I am closing the thread for now.

Do not hesitate to contact us should you have any other questions on our controls.

Best Regards,
Stefan
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.

 
Tags
Carousel
Asked by
Stephan
Top achievements
Rank 1
Answers by
Yoan
Telerik team
Stephan
Top achievements
Rank 1
Tony
Top achievements
Rank 1
Stefan
Telerik team
Share this question
or