Tuesday, January 22, 2013

Selected RadioButton from ListBox Contents WindowsPhone8

Selected RadioButton from ListBox Contents WindowsPhone8

I have placed a Radio button in a listbox content and binded it with list of QuizOption1 binding is working fine and showing the radio button checked if the property IsSelected is passed as true. the class definition is given below.

    class QuizOption1     {         public int QuizID { get; set; }         public int QuizOptionID { get; set; }         public string Description { get; set; }         public bool IsSelected { get; set; }     } 

While checking for the checked items, i am using following code

            var lstItems = (List<QuizOption1>)lst.ItemsSource;             var selItems = lstItems.Where(op => op.IsSelected == true).FirstOrDefault(); 

The binding is as follows.

<ListBox  Name="lst1" Grid.Row="1" >                         <ListBox.ItemTemplate >                             <DataTemplate >                                 <RadioButton                                      Foreground="#333333"                                     Background="#ffededed"                                      Tag="{Binding QuizOptionID}"                                     Content="{Binding Description}"                                     IsEnabled="True"                                     GroupName="{Binding QuizID}"                                     IsChecked="{Binding Path=IsSelected}"/>                             </DataTemplate>                         </ListBox.ItemTemplate>                     </ListBox> 

but selItems is always null. Can anyone tell me why? Thanks in advance.

Answers & Comments...

Answer: 1

The answer is very simple, i need to add the Mode=TwoWay attribute in binding and binding looks like following. Thanks anyways.

IsChecked="{Binding IsSelected, Mode=TwoWay}"/> 
by : LojiSmith Kaleemhttp://stackoverflow.com/users/1929889




No comments:

Post a Comment

Send us your comment related to the topic mentioned on the blog