RadRadioButton selection in the ListBox

8 posts, 0 answers
  1. Dragan
    Dragan avatar
    5 posts
    Member since:
    Apr 2008

    Posted 29 Jan 2010 Link to this post

    I have a very simple scenario but for some reason I can't think of how to solve it. I have a ListBox that defines a DataTemplate consisting of RadRadioButtons. The ItemsSource is tied to a query from Entity Framework. What I need to do is select the first item in the list after the list has been loaded. This is fine by simply using SelectedIndex property. However, I also want to set IsChecked property of the RadRadioButton to true for the same item. I am not sure how to go about this. When I look at the SelectedItem property I get the underlying datasource which is my entity, but how do I reference the RadRadioButton?
    Thank you in advance.
  2. Valentin.Stoychev
    Admin
    Valentin.Stoychev avatar
    2198 posts

    Posted 01 Feb 2010 Link to this post

    Hi Dragan,

    You can simply bind the IsChecked property of the RadioButton to the property of your business object. You can do this directly in the DataTemplate,  you don't need to reference the radio button explicitely.

    <DataTemplate>
    ...
    <telerik:RadRadioButton IsChecked="{Binding IsSelected}" .../>
    ...
    </DataTemplate>

    Please let us know if you still have any problems. You can send us a the sample code that is not working.

    Regards,
    Valentin.Stoychev
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  3. DevCraft banner
  4. Dragan
    Dragan avatar
    5 posts
    Member since:
    Apr 2008

    Posted 01 Feb 2010 Link to this post

    Thank you for your reply. I understand that I can bind IsChecked property directly but that is not my issue here. My page has a master-detail relationship setup with the ListBox serving as a master and the grid as a detail. What I'm trying to do is when the ListBox loads its contents to simply select the first item in the list (programmatically) which would then populate the Grid. This is something th users have requested. What I did up to this point is hook into ItemContainerGenerator_ItemsChanged event and check the list count. When the list count equals 1, I set the ListBox's SelectedIndex to 0. But, in addition to that I want to set IsChecked property to true so that the user can see which item master item is selected. I hope that describes my scenario a little bit better.
  5. Tina Stancheva
    Admin
    Tina Stancheva avatar
    3298 posts

    Posted 03 Feb 2010 Link to this post

    Hello Dragan,

    Please take a look at the sample project that I prepared for you and let me know if it helps.

    It demonstrates how to keep the state of the radio button and the selected item in the listbox in synch.
     
    Please let us know if your scenario requires an approach different than the one on the sample.

    Regards,
    Tina Stancheva
    the Telerik team

    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
    Follow the status of features or bugs in PITS and vote for them to affect their priority.
  6. Dragan
    Dragan avatar
    5 posts
    Member since:
    Apr 2008

    Posted 16 Feb 2010 Link to this post

    Thank you for your reply. Your solution correctly depicts my scenario. My question is, how would you do the same thing without relying on IsChecked property in the business object? My data is coming from the database and I am using entity framework. I don't want to extend the entity with IsChecked property just so it can be selected on the screen. Is there any way to do this without extending the business object?
  7. Tina Stancheva
    Admin
    Tina Stancheva avatar
    3298 posts

    Posted 17 Feb 2010 Link to this post

    Hello Dragan,

    I modified the listBox_SelectionChanged() method in the example and removed the IsChecked property in the business object.

    Please take a look at the new example and let us know if you need more info.

    All the best,
    Tina Stancheva
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  8. Dragan
    Dragan avatar
    5 posts
    Member since:
    Apr 2008

    Posted 17 Feb 2010 Link to this post

    Thank you very much for your solution. I think the solution is elegant and straight forward. I would have never thought about using 

    listBox.ItemContainerGenerator.ContainerFromItem(e.AddedItems[0])

    as System.Windows.Controls.ListBoxItem to get a reference to the ListBoxItem. So, thank you for that insight. I have also found another solution that uses the Loaded event of the UIControl defined in the DataTemplate to apply custom logic once the control has been loaded.

     

  9. NInoska
    NInoska avatar
    1 posts
    Member since:
    Feb 2011

    Posted 01 Feb 2011 Link to this post

     List<misdatos> sele = new List<misdatos>();

     private void list_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {

                if (e.RemovedItems.Count!=0)
                {
                    sele.Remove(e.RemovedItems[0] as misdatos);
                    txtseleccion.Text = sele.Count.ToString();
                }
                if (e.AddedItems.Count != 0)
                {
                    sele.Add(e.AddedItems[0] as misdatos);
                    txtseleccion.Text = sele.Count.ToString();
                }

              

            }

Back to Top
DevCraft banner