【发布时间】:2010-12-02 12:07:01
【问题描述】:
我正在构建的一个应用程序需要构建一个供用户进行的调查。与我的问题相关的相关课程是:
Survey (string Description, List<Questions> Questions, QuestionTypes Type)
Question (string Description, List<Choice> Choices)
Choice (string Description)
enum QuestionTypes {MultipleChoicesOneAnswer, MultipleChoicesMultipleAnswers}
在 ListBox 中,我想在自己的边框中显示每个问题以及每个问题的可用选项。像这样:
1) 一天有几个小时?
[ ] 21
[ ] 22
[ ] 23
[ ] 24
上面的括号代表一个单选按钮(如果问题类型是“MultipleChoicesOneAnswer”)或复选框(对于“MultipleChoicesMultipleAnswers”类型的问题)
我正在使用以下模板:
<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>
使用这些模板,我会为每个选择获得一个 RadioButton,但它们并不是每个问题都互斥的。
我的问题是:
1、如何使模板生成的RadioButtons互斥?
2. 我将如何将用户选择的选项记录到我的模型中?
非常感谢您的帮助。
更新: 按照 csunwold 的建议,它使用 GroupName 来工作,但感觉有点像 hack,因为我必须在 Question 类上重写 ToString() 并吐出描述并使用 GroupName="{Binding Question}"。
其他人有什么建议吗?
【问题讨论】:
标签: wpf xaml listbox radio-button datatemplate