Multiple RadioButton Groups in ItemsControl

I am working on a small project that displays survey responses. I have a problem displaying answers to questions about options.

As you can see in the xaml statement below, I am trying to group the switch by response ID, so only one parameter is selected per response object.

However, the code below applies to the entire switch in the entire survey as part of one large group of radio objects and allows you to select only one option for all questions.

Let's say I have 2 responses to display ( -= not selected, += selected):

I expect something like this:

Answer1:

-Option1 - Option2 + Option3

Answer2:

-Option1 + Option2 - Option3

But the xaml code below allows me to have only one selected value from both questions instead of causing mutual exclusivity to the question.

<ItemsControl ItemsSource="{Binding Options}">
                <ItemsPanelTemplate>
                    <StackPanel Orientation="Horizontal" />
                </ItemsPanelTemplate>
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <RadioButton GroupName="{Binding AnswerId}" Content="{Binding Option}" IsChecked="{Binding IsSelected, Mode=OneWay}"/>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
+5
source share
2 answers

I created a test using your Xaml and the following code, and it works fine (allows one choice from each group of 3 answers):

enter image description here

Do you create all response parameters before binding? It appears that GroupName is not a dependency property.

using System.Collections.Generic;
namespace PersonTests
{
    public class QuestionTestViewModel
    {
        public IEnumerable<AnswerOption> Options { get; set; }

        public QuestionTestViewModel()
        {
            this.Options = new List<AnswerOption>()
                            {
                                new AnswerOption(){AnswerId = 1, Option = "One A", IsSelected = false},
                                new AnswerOption(){AnswerId = 1, Option = "One B", IsSelected = false},
                                new AnswerOption(){AnswerId = 1, Option = "One C", IsSelected = false},
                                new AnswerOption(){AnswerId = 2, Option = "Two A", IsSelected = false},
                                new AnswerOption(){AnswerId = 2, Option = "Two B", IsSelected = false},
                                new AnswerOption(){AnswerId = 2, Option = "Two C", IsSelected = false}
                            };
        }
    }

    public class AnswerOption
    {
        public int AnswerId { get; set; }
        public string Option { get; set; }
        public bool IsSelected { get; set; }
    }
}
+1
source

Perhaps you can post the definition of the object / class to which you are attached, it is a little unclear how your data structure works. I built a Silverlight questionnaire, had a similar task ...

0
source

All Articles