WPF - How do I make the RadioButtons generated by the DataTemplate mutually exclusive?

In one of the applications I am building, I need to create a survey for users. The relevant classes related to my question are as follows:

 Survey (string Description, List<Questions> Questions, QuestionTypes Type)
 Question (string Description, List<Choice> Choices)
 Choice (string Description)
 enum QuestionTypes {MultipleChoicesOneAnswer, MultipleChoicesMultipleAnswers}

      

In a ListBox, I want to display each question on its own border and the choices available for each question. For example:
1) How many hours a day?
[] 21
[] 22
[] 23
[] 24

The parentheses above represent a radio button (if the question type is "MultipleChoicesOneAnswer") or a checkbox (for questions like "MultipleChoicesMultipleAnswers")

I am using the following templates:

<DataTemplate x:Key="MultipleChoicesOneAnswerTemplate" DataType="{x:Type local:Choice}">
   <RadioButton Content="{Binding Description}" Foreground="Blue"  />
</DataTemplate>


<DataTemplate x:Key="QuestionTemplate" DataType="{x:Type local:Question}">
   <DockPanel>
      <StackPanel Orientation="Horizontal" DockPanel.Dock="Top">
         <TextBlock Text="{Binding SortingIndex}" />
         <TextBlock Text=")" />
         <TextBlock Text="{Binding Description}" Margin="5,0,0,0"/>
      </StackPanel>
      <ListBox ItemTemplate="{StaticResource MultipleChoicesOneAnswerTemplate}" ItemsSource="{Binding Choices}"/>
   </DockPanel>
</DataTemplate>

      

With these templates, I get a RadioButton for each selection, but they are not mutually exclusive for each question.

My questions for you:

 1. What can I do to make the RadioButtons generated by the template mutually exclusive?
 2. How do I navigate to the entry in my model that the user has selected?

Many thanks for your help.

UPDATE: Following csunwold's suggestion, it worked using GroupName, but it looks a bit like a hack as I had to override ToString () in the Question class, spitting out the description, and GroupName = "{Binding Question}".

Anyone have any suggestions?

+2


source to share


1 answer


Give the radio buttons you want to mutually exclude the same GroupName property. You can probably write this to your model, but create one type of enum that gets updated every time a single radio object is checked.



+2


source







All Articles