开发者

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

I'm building a qu开发者_运维问答estionnaire 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.

The XAML looks like this

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

The ItemTemplateSelector choose the ItemTemplate based on the Property ComboBox specified in Question object. One ItemTemplate shows the String[] choices inside a ComboBox, the other create as many TextBox as the number of Strings in the array.

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. 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜