How do you read/initialize the selected item?

9 posts, 0 answers
  1. Perry
    Perry avatar
    3 posts
    Member since:
    Oct 2011

    Posted 15 Dec 2011 Link to this post

    Hello,

    I'm trying to implement a simple integer loopinglist.  I'd like to initialize the starting values for a simple loopinglist, and or be able to read the item, before the user has started selection.  From the sparse documentation, I guess the method SelectedIndexChanged is used to read the item after it's been selected, but if I want to read and or initialize the value (during the constructor) I don't know what to use.  BTW, I can read the values after spinning the looping list.

    -Regards,
    Perry
  2. Todor
    Admin
    Todor avatar
    778 posts

    Posted 15 Dec 2011 Link to this post

    Hi Perry,

    Thank you for your question.

    Could you please elaborate a bit more on what is missing in our documentation so that we can update it accordingly.

    To set the items for RadLoopingList, you need to create a LoopingListDataSource. Then you need to create event handlers for the two events that get fired during the initialization of the list: ItemNeeded and ItemUpdated. Inside the event handlers you can define your own logic and initialize the values as you wish. If you want to read a specific item before the user selected it, you can use the method GetItemAt(index) for the DataSource of the LoopingList. You can also find detailed looping list examples in our Demo application, available in the installation package.

    I hope this information will be useful. However, if you would like to implement a different scenario, I would kindly like to ask you to share some more details on it, so I could assist you further.

    Greetings,
    Todor
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  3. DevCraft banner
  4. Perry
    Perry avatar
    3 posts
    Member since:
    Oct 2011

    Posted 16 Dec 2011 Link to this post

    Hi Todor,

    Thanks for the quick response.  It never occurred to me that I should initialize the Radloopinglist in the data source.  I didn't see in the documentation that two event handlers were fired during initialization of the list.  When I looked (again), I couldn't figure out how to do what I wanted,  This is all that I could find for the methods that you mentioned.

    -------------
    LoopingListDataSource is fully virtualized which is why data items are created in the ItemNeeded event handler. A data item is created only if it is going to be visualized. 


    The basic LoopingListDataItem supports only a text property. In order to add more properties users need to inherit from LoopingListDataItem and add the necessary information.

    Then they can simply create an instance of their custom data item in the ItemNeeded event handler.

    If the view port of the looping list is not big enough to display all of the data, which is almost always the case, users need to subscribe to the ItemUpdated event. ItemUpdated is called whenever the looping list needs to display new data, that is, data that is not visible at the moment, but that will be visualized shortly. For example when the looping list scrolls the items from the inertia caused by a user flick, the ItemUpdated event will be fired for items that will be visualized next. The arguments in the event handler provide the data item that has to be visualized and an index that corresponds to the real data record in the original data source. Users are expected to update the data item with the real data from their own data source. A code snippet will best illustrate this:

    --------------------

    I'm not sure that explains what I need to do.  To clarify what I need, I have a RadLoopingList (which i've set up with a datasource) that cycles through 0-9, simple integers (yes, I know it's displayed as text).  Upon startup, (initialization) I want a specific number to show on the list, it could be 3, 4,5, whatever, it will come from a variable somewhere.  Is this possible with the RadLoopingList?

    A question about the description for ItemUpdated, how do you subscribe to that event? Can you provide some example code to illustrate how this should be used?

    Another question, which may not be as significant, what does 'fully virtualized' mean?  Is this a c# concept?  I'm new with c#.

    -Regards,
    Perry
  5. Todor
    Admin
    Todor avatar
    778 posts

    Posted 19 Dec 2011 Link to this post

    Hi Perry,

    Virtualization is actually not only a C# concept, but a concept in computing in general. In basic, it is the creation of a virtual (rather than actual) version of the data. In RadLoopingList the data comes from a LoopingListDataSource and the LoopingList contains the virtual containers for the data. In your scenario, you have 10 items, but the virtual containers are less. That means that upon initialization the containers that are needed (i.e. ItemNeeded is fired) are created so that they fill the screen of the device and their content is updated (i.e. ItemUpdated is fired). When the flick gesture occurs the same containers are used to represent the new data and only their content is updated (only the ItemUpdated is fired). Having in mind this you can create your logic for these two events. For example, for the digits from 0 to 9, starting from the value of a variable named startValue, the event handlers could look like this:


    void dataSource_ItemNeeded(object sender, LoopingListDataItemEventArgs e)
    {
        e.Item = new LoopingListDataItem();
    }
     
    void dataSource_ItemUpdated(object sender, LoopingListDataItemEventArgs e)
    {
        e.Item.Text = ((e.Index + startValue) % 10).ToString();
    }


    And here is how you create a data source with 10 items and subscribe for the events:

    LoopingListDataSource dataSource = new LoopingListDataSource(10);
    dataSource.ItemNeeded += new EventHandler<LoopingListDataItemEventArgs>(dataSource_ItemNeeded);
    dataSource.ItemUpdated += new EventHandler<LoopingListDataItemEventArgs>(dataSource_ItemUpdated);


    If you want a specific item from the list to be selected before the user has tapped on it, you just have to set the SelectedIndex property of the RadLoopingList after you initialize it.

    I hope this will help you. If you have any other questions, don't hesitate to write us back.

    All the best,
    Todor
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  6. jasonxz
    jasonxz avatar
    17 posts
    Member since:
    Jul 2012

    Posted 10 Sep 2012 Link to this post

    Todor,

    In the previous post, you stated that to select an item in the LoopingList for the user, just set the SelectedIndex property of the LoopingList.  I am attempting to do that through Two-Way DataBinding.  The selecting is working fine, but the problem I'm having is that the selected item doesn't move into the viewport.  If I manually scroll, I can see that the appropriate item is selected, but I can't figure out how to make that item automatically visible.
  7. Todor
    Admin
    Todor avatar
    778 posts

    Posted 11 Sep 2012 Link to this post

    Hello Jason,

    Thank you for contacting us.

    The SelectedIndex is used exactly for the selection and it is not supposed to make the SelectedItem visible. In order to do this, you need to call the BringIntoView method after the Looping list is loaded:
    loopingList.Loaded += this.OnLoopingListLoaded;
    And here is the OnLoopingListLoaded:
    void OnLoopingListLoaded(object sender, RoutedEventArgs e)
    {
        loopingList.BringIntoView(loopingList.SelectedIndex);
    }
    I hope this information helps. Let me know if you need additional assistance.

    Greetings,
    Todor
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  8. jasonxz
    jasonxz avatar
    17 posts
    Member since:
    Jul 2012

    Posted 12 Sep 2012 Link to this post

    Thank you Todor for your response.
    The problem with the solution you have provided is that, when the Loaded event fires for the LoopingList, I haven't created the LoopingListDataSource, yet.
    What I'm doing is retrieving the data on a background thread, so the UI actually loads before the data is applied to it.  So, when the data arrives, I create the LoopingListDataSource, apply it to the LoopingList and set the SelectedIndex.
    So, I assume I need to make a call between setting the LoopingList's DataSource and the SelectedIndex.  I have no idea what that is, though.
    Can you help?
  9. Todor
    Admin
    Todor avatar
    778 posts

    Posted 13 Sep 2012 Link to this post

    Hello Jason,

    The call you mean is the BringIntoView method. Since the data is not loaded when the LoopingList's Loaded event is fired, you can use the LoopingList's SelectedIndexChanged Event and call the BringIntoView on Dispatcher's BeginInvoke:
    private void OnLoopingListSelectedIndexChanged(object sender, EventArgs e)
    {
        this.Dispatcher.BeginInvoke(() =>
            {
                this.loopingList.BringIntoView(this.loopingList.SelectedIndex);
            });
    }
    I hope this information helps. Let me know if you need additional assistance.

    Greetings,
    Todor
    the Telerik team

    Time to cast your vote for Telerik! Tell DevPro Connections and Windows IT Pro why Telerik is your choice. Telerik is nominated in a total of 25 categories.

  10. jasonxz
    jasonxz avatar
    17 posts
    Member since:
    Jul 2012

    Posted 18 Sep 2012 Link to this post

    It seems to be working, now.
    Thanx.
Back to Top
DevCraft banner