简体   繁体   English

在WPF DataTemplate中访问随机数量的控件的最佳方法是什么

[英]What's the best way of access a random number of controls in WPF DataTemplate

I'm building a questionnaire application that shows a series of questions to the user. 我正在构建一个问卷调查应用程序,向用户显示一系列问题。

To simplify the model, the question is represented by 为了简化模型,问题用

class Question{
    String name;
    bool ComboBox;
    String[] choices;
}

In the code-behind I have a ObservableCollection that is used by the DataContext. 在后面的代码中,我有一个由DataContext使用的ObservableCollection。

The XAML looks like this XAML看起来像这样

...
<ItemsControl ItemsSource="{Binding}" ItemTemplateSelector="{...}">
</ItemsControl>

The ItemTemplateSelector choose the ItemTemplate based on the Property ComboBox specified in Question object. ItemTemplateSelector根据Question对象中指定的属性ComboBox选择ItemTemplate。 One ItemTemplate shows the String[] choices inside a ComboBox, the other create as many TextBox as the number of Strings in the array. 一个ItemTemplate显示ComboBox内的String []选择,另一个ItemTemplate创建与数组中String数量一样多的TextBox。

What's the best way to gather all the data inserted by the user. 收集用户插入的所有数据的最佳方法是什么。

The simplest way is to have the answer(s) in your Question class as well. 最简单的方法是在Question类中也有答案。 After the user fills the questionnaire, just iterate through you collection to get the answers. 用户填写调查表后,只需遍历您的集合即可获得答案。 Add one more property such as AnsweredIndex, to save the index chosen by the user. 再添加一个属性,例如AnsweredIndex,以保存用户选择的索引。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM