使用动态创建的控件跟踪值

时间:2011-06-14 21:26:04

标签: c# .net wpf vb.net

我有一个WPF应用程序,允许用户创建所有不同类型的问题,根据类型将使用文本框,组合框,复选框或单选按钮,以允许用户在他们构建某种问卷后回答问题。我的问题是,在创建控件并动态创建调查问卷后,在所有不同控件上跟踪答案的最佳方法是什么。现在,我循环遍历所有容器并根据controlType获取值。哪个工作正常,但我想知道数据绑定或其他什么会以某种方式为我提供更好的解决方案。

我对数据绑定的打嗝是,在创建所有内容之前,我没有预期答案或问题的结构,因此每次都可能不同。我知道这有点模糊,但我真的很感激任何人都可以提供帮助。感谢。

1 个答案:

答案 0 :(得分:4)

Id创建表示每种类型问题的类(即需要测试框答案的类,一组用于组合框等)。还要创建一个datatemplateselector,以选择所需的模板,并在xaml中为此选择器创建资源。

类:

public abstract class QuestionType
{
    public string Question { get; set; }
}

public class TextBoxQuestion : QuestionType
{
    public string Answer { get; set; }
}

public class CheckBoxQuestion : QuestionType
{
    public bool Answer { get; set; }
}

public class ComboBoxQuestion : QuestionType
{
    public List<string> Values { get; set; }

    public string Answer { get; set; }
}

public class QuestionTemplateSelector : DataTemplateSelector
{
    public DataTemplate Combo { get; set; }
    public DataTemplate Text { get; set; }
    public DataTemplate Check { get; set; }

    public override System.Windows.DataTemplate SelectTemplate(object item, System.Windows.DependencyObject container)
    {
        if (item is TextBoxQuestion) return Text;
        if (item is ComboBoxQuestion) return Combo;
        if (item is CheckBoxQuestion) return Check;
        return null;
    }
}

实例化代码:

    public MainWindow()
    {
        InitializeComponent();

        ObservableCollection<QuestionType> Questions = new ObservableCollection<QuestionType>()
        {
            new TextBoxQuestion() { Question = "What's your favorite color?" },
            new CheckBoxQuestion() { Question = "Are you allergic to peanuts?" },
            new ComboBoxQuestion() { Question = "How many fingers am I holding up?",
                Values = new List<string>() { "1", "2", "3", "4", "6" }}
        };

        QuestionList.ItemsSource = Questions;
    }

XAML:

<Grid>
    <Grid.Resources>
        <local:QuestionTemplateSelector x:Key="questionSelector">
            <local:QuestionTemplateSelector.Check>
                <DataTemplate DataType="local:CheckBoxQuestion">
                    <StackPanel>
                        <TextBlock Text="{Binding Question}"/>
                        <CheckBox IsChecked="{Binding Answer}"/>
                    </StackPanel>
                </DataTemplate>
            </local:QuestionTemplateSelector.Check>
            <local:QuestionTemplateSelector.Text>
                <DataTemplate DataType="local:TextBoxQuestion">
                    <StackPanel>
                        <TextBlock Text="{Binding Question}"/>
                        <TextBox Text="{Binding Answer}"/>
                    </StackPanel>
                </DataTemplate>
            </local:QuestionTemplateSelector.Text>
            <local:QuestionTemplateSelector.Combo>
                <DataTemplate DataType="local:ComboBoxQuestion">
                    <StackPanel>
                        <TextBlock Text="{Binding Question}"/>
                        <ComboBox SelectedValue="{Binding Answer}" ItemsSource="{Binding Values}"/>
                    </StackPanel>
                </DataTemplate>
            </local:QuestionTemplateSelector.Combo>
        </local:QuestionTemplateSelector>
    </Grid.Resources>
    <ListBox Name="QuestionList" ItemTemplateSelector="{StaticResource questionSelector}"/>
</Grid>