需要使用与外部模板相同的内部数据模板

时间:2014-06-26 21:12:02

标签: wpf datatemplate

我有以下XML结构(简化为例),您可以在问题中提出问题。还有其他类型的问题,所以我决定制作一个模板来显示它们,我可以创建最高级别的问题显示,但是当我到达内层时,我无法显示我的模板。除了“字符串”之外,我还有其他问题类型,但我删除了它们以尝试专注于手头的问题。

XML结构

<QUESTIONS>
    <QUESTION id="3" text="What is this?" type="string">
        <QUESTIONS>
            <QUESTION id="7" text="What is the First Inner Question?" type="string">
                <QUESTIONS>
                    <QUESTION id="8" text="What is another inner Question?" type="string">
                    </QUESTION>
                </QUESTIONS>
            </QUESTION>
        </QUESTIONS>
    </QUESTION>
    <QUESTION id="4" text="Where another question?" type="string">
        <QUESTIONS>
            <QUESTION id="5" text="What is another inner question?" type="string">
                <QUESTIONS>
                    <QUESTION id="6" text="What is another inner question?" type="string">
                    </QUESTION>
                </QUESTIONS>
            </QUESTION>
        </QUESTIONS>
    </QUESTION>
  </QUESTIONS>

我的XAML

<Window.Resources>

    <DataTemplate x:Key="StringQuestionTemplate" DataType="models:FormQuestion" >

        <StackPanel Orientation="Horizontal" >
            <TextBlock Text="{Binding Path=QuestionText}" />
            <TextBox  Text="{Binding Path=Answer, Mode=TwoWay}" />

            <StackPanel>
                <ItemsControl ItemsSource="{Binding Path=InnerQuestions}" ItemTemplateSelector="{StaticResource TemplateSelector}" />
            </StackPanel>

        </StackPanel>
    </DataTemplate>

    <templateIssue:QuestionTemplateSelector x:Key="TemplateSelector" 
                                    StringTemplate="{ StaticResource StringQuestionTemplate }" />

</Window.Resources>

    <Grid>
        <StackPanel Orientation="Vertical">
            <ItemsControl ItemsSource="{Binding Path=Questions}" ItemTemplateSelector="{StaticResource TemplateSelector}" >
            </ItemsControl>
        </StackPanel>
    </Grid>

这是QuestionTemplateSelector

public class QuestionTemplateSelector : DataTemplateSelector
{
    public DataTemplate StringTemplate { get; set; }

    public override DataTemplate SelectTemplate(object item, DependencyObject container)
    {
        switch (((FormQuestion)item).Type)
        {
            case QuestionType.StringQuestion:
                return StringTemplate;
            default:
                return StringTemplate;
        }
    }
}

我想为原始模板使用与“InnerQuestions”相同的模板选择器,但是因为它在我无法执行的部分之后被引用(抛出错误)。所以我想在“InnerQuestions”中使用相同的DataTemplate,就像我对顶级问题一样。有没有办法实现这一点,或者使用不同的结构进行模板化?

1 个答案:

答案 0 :(得分:1)

听起来你需要使用HierarchicalDataTemplate Class。也许这样的事情(假设你的Binding是正确的):

<HierarchicalDataTemplate DataType="models:FormQuestion"
    ItemsSource="{Binding Path=InnerQuestions}"
    ItemTemplateSelector="{StaticResource TemplateSelector}">
    <StackPanel Orientation="Horizontal" >
        <TextBlock Text="{Binding Path=QuestionText}" />
        <TextBox Text="{Binding Path=Answer, Mode=TwoWay}" />
    </StackPanel>
</DataTemplate>

更新&gt;&gt;&gt;

您还需要使用可以显示分层数据的控件,例如TreeView

<TreeView ItemsSource="{Binding TopLevelQuestions}" />