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

C# 使用动态创建的控件跟踪值,c#,.net,wpf,vb.net,C#,.net,Wpf,Vb.net,我有一个WPF应用程序,允许用户创建所有不同类型的问题,根据类型,将使用文本框、组合框、复选框或单选按钮,允许用户在构建某种问卷后回答问题。我的问题是,在创建控件并动态创建问卷后,跟踪所有不同控件的答案的最佳方法是什么。现在,我正在遍历所有容器并获取基于controlType的值。这很好,但我想知道数据绑定或其他方式是否能为我提供更好的解决方案 我在数据绑定方面遇到的麻烦是,在创建所有内容之前,我没有预期答案或问题的结构,因此每次都可能不同。我知道这有点含糊不清,但我真的很感激任何人能提供的帮助

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


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

我创建了代表每种问题类型的类(即需要测试框答案的类,组合框答案的类,等等)。还要创建一个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;
    }
}
公共抽象类问题类型
{
公共字符串问题{get;set;}
}
公共类TextBoxQuestion:QuestionType
{
公共字符串答案{get;set;}
}
公共类复选框问题:问题类型
{
公共bool应答{get;set;}
}
公共类ComboBoxQuestion:QuestionType
{
公共列表值{get;set;}
公共字符串答案{get;set;}
}
公共类问题TemplateSelector:DataTemplateSelector
{
公共数据模板组合{get;set;}
公共数据模板文本{get;set;}
公共数据模板检查{get;set;}
公共覆盖System.Windows.DataTemplate SelectTemplate(对象项,System.Windows.DependencyObject容器)
{
如果(项目为TextBoxQuestion)返回文本;
如果(项目是ComboBoxQuestion)返回Combo;
如果(项目为CheckBoxQuestion)返回检查;
返回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;
    }
public主窗口()
{
初始化组件();
ObservableCollection问题=新的ObservableCollection()
{
新建TextBoxQuestion(){Question=“你最喜欢什么颜色?”},
new CheckBoxQuestion(){Question=“你对花生过敏吗?”},
new ComboBoxQuestion(){Question=“我能举起多少根手指?”,
值=新列表(){“1”、“2”、“3”、“4”、“6”}
};
QuestionList.ItemsSource=问题;
}
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>