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

RadRadioButton selection in the ListBox

7 Answers 230 Views
Buttons
This is a migrated thread and some comments may be shown as answers.
Dragan
Top achievements
Rank 1
Dragan asked on 29 Jan 2010, 04:36 PM
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.

7 Answers, 1 is accepted

Sort by
0
Valentin.Stoychev
Telerik team
answered on 01 Feb 2010, 07:50 AM
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.
0
Dragan
Top achievements
Rank 1
answered on 01 Feb 2010, 01:51 PM
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.
0
Tina Stancheva
Telerik team
answered on 03 Feb 2010, 12:54 PM
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.
0
Dragan
Top achievements
Rank 1
answered on 16 Feb 2010, 03:22 PM
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?
0
Tina Stancheva
Telerik team
answered on 17 Feb 2010, 03:47 PM
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.
0
Dragan
Top achievements
Rank 1
answered on 17 Feb 2010, 05:35 PM
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.

 

0
NInoska
Top achievements
Rank 1
answered on 01 Feb 2011, 05:44 PM

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

          

        }

Tags
Buttons
Asked by
Dragan
Top achievements
Rank 1
Answers by
Valentin.Stoychev
Telerik team
Dragan
Top achievements
Rank 1
Tina Stancheva
Telerik team
NInoska
Top achievements
Rank 1
Share this question
or