Why is getting the SelectedItem so Hard?

7 posts, 0 answers
  1. Stephan
    Stephan avatar
    33 posts
    Member since:
    Jun 2012

    Posted 16 Jan 2013 Link to this post

    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);
                 
            }

     

  2. Yoan
    Admin
    Yoan avatar
    1068 posts

    Posted 17 Jan 2013 Link to this post

    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.

  3. UI for WPF is Visual Studio 2017 Ready
  4. Stephan
    Stephan avatar
    33 posts
    Member since:
    Jun 2012

    Posted 17 Jan 2013 Link to this post

    Yes, I figured it out last night.  I could have kicked myself.
  5. Tony
    Tony avatar
    24 posts
    Member since:
    Sep 2014

    Posted 16 Feb 2015 Link to this post

    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.














  6. Stefan X1
    Admin
    Stefan X1 avatar
    517 posts

    Posted 19 Feb 2015 Link to this post

    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.

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

    Posted 19 Feb 2015 in reply to Stefan X1 Link to this post

    Thanks for the suggestion. I'll give it try and let you know how it goes.

    Thanks.

    Tony
  8. Stefan X1
    Admin
    Stefan X1 avatar
    517 posts

    Posted 24 Feb 2015 Link to this post

    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.

     
Back to Top
UI for WPF is Visual Studio 2017 Ready