【问题标题】:Loading Different DataTemplate for each item in ListBox为 ListBox 中的每个项目加载不同的 DataTemplate
【发布时间】:2014-01-14 10:57:28
【问题描述】:

我正在尝试创建一个学习应用程序,我想根据问题类型加载数据模板 如下所述。

     If Question Type is TYPE1

     load InstructionTemplate_Type1.xaml
     load ChoiceTemplate_Type1.xaml
     load QuestionTemplate_Type1.xaml

     If Question Type is TYPE2

     load InstructionTemplate_Type2.xaml
     load ChoiceTemplate_Type2.xaml
     load QuestionTemplate_Type2.xaml

     If Question Type is TYPE3

     load InstructionTemplate_Type3.xaml
     load ChoiceTemplate_Type3.xaml
     load QuestionTemplate_Type3.xaml

     else

     load InstructionTemplate_Type3.xaml
     load ChoiceTemplate_Type3.xaml
     load QuestionTemplate_Type3.xaml

我的页面应该看起来像...

谁能帮我怎么做。

我正在使用上一篇文章中的代码

Nested ObservableCollection data binding in WPF

xaml 是 ...

    <learn:SelectedItemIsCorrectToBooleanConverter x:Key="SelectedCheckedToBoolean" />

    <Style x:Key="ChoiceRadioButtonStyle" TargetType="{x:Type RadioButton}" BasedOn="{StaticResource {x:Type RadioButton}}">
        <Style.Triggers>
            <DataTrigger Value="True">
                <DataTrigger.Binding>
                    <MultiBinding Converter="{StaticResource SelectedCheckedToBoolean}">
                        <Binding Path="IsCorrect" />
                        <Binding RelativeSource="{RelativeSource Self}" Path="IsChecked" />
                    </MultiBinding>
                </DataTrigger.Binding>
                <Setter Property="Background" Value="Green"></Setter>
            </DataTrigger>
            <DataTrigger Value="False">
                <DataTrigger.Binding>
                    <MultiBinding Converter="{StaticResource SelectedCheckedToBoolean}">
                        <Binding Path="IsCorrect" />
                        <Binding RelativeSource="{RelativeSource Self}" Path="IsChecked" />
                    </MultiBinding>
                </DataTrigger.Binding>
                <Setter Property="Background" Value="Red"></Setter>
            </DataTrigger>
        </Style.Triggers>
    </Style>

    <DataTemplate x:Key="InstructionTemplate" DataType="{x:Type learn:Question}">
        <TextBlock Text="{Binding Path=Instruction}" />
    </DataTemplate>

     <DataTemplate x:Key="ChoiceTemplate" DataType="{x:Type learn:Choice}">
                        <RadioButton Content="{Binding Path=Name}" IsChecked="{Binding RelativeSource=   {RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}}, Path=IsSelected}" Margin="10 1" 
                                     Style="{StaticResource ChoiceRadioButtonStyle}" />
                    </DataTemplate>

    <DataTemplate x:Key="QuestionTemplate" DataType="{x:Type learn:Question}">
        <StackPanel Margin="10 0">
            <TextBlock Text="{Binding Path=Name}" />
            <ListBox ItemsSource="{Binding Path=Choices}" SelectedItem="{Binding Path=SelectedChoice}" HorizontalAlignment="Stretch" ItemTemplate="ChoiceTemplate">

            </ListBox>
        </StackPanel>
    </DataTemplate>
</Window.Resources>

<DockPanel>
    <StackPanel Orientation="Horizontal" DockPanel.Dock="Bottom">
        <Button Content="Select Question 3 choice 3" Click="ButtonBase_OnClick" />
    </StackPanel>
    <ItemsControl ItemsSource="{Binding Path=Questions}">
        <ItemsControl.ItemTemplateSelector>
            <learn:QuestionTemplateSelector QuestionTemplate="{StaticResource QuestionTemplate}" InstructionTemplate="{StaticResource InstructionTemplate}" />
        </ItemsControl.ItemTemplateSelector>
    </ItemsControl>
</DockPanel>

有人可以帮助我了解如何通过更智能的设计来存档吗 (可能是问题的通用基类,并且每个问题类型都有派生问题类,并使用类中的虚函数加载数据模板......)但我想知道如何使用模板选择器来完成......或我们是否需要使用一些不同的方法..

【问题讨论】:

    标签: c# wpf xaml listbox


    【解决方案1】:

    如果您创建从通用 Quiestion ViewModel 派生的 ViewModel-s,则可以创建列表 (ObservableCollection&lt;Question&gt;)。然后使用下面的 ListBox:

    <ListBox ItemsSource="{Binding YourQuestionList}">
         <ListBox.Resources>
               <DataTemplate DataType="{x:Type VM:QuestionType1}">
                      ( ... question1 full design ... )
               </DataTemplate>
               <DataTemplate DataType="{x:Type VM:QuestionType2}">
                      ( ... question2 full design ... )
               </DataTemplate>
               ( ... other data templates ... )
    </ListBox>
    

    DataContext 将是您自定义完整设计中的特定 Question ViewModel,因此您也可以使用这些属性进行绑定。您需要将对 ViewModel 所在的命名空间的引用(例如:xmlns:VM="clr-namespace:YourApp.VMs")添加到 xaml 文件的顶部(如果您的 ViewModels 的命名空间是 VMs

    我认为这应该为你工作。

    【讨论】:

    • 谢谢鲁道夫。问题是我也需要单独的指令和选择模板。在这种情况下,我认为我需要 ItemTemplateSelector。我希望 ItemTemplateSelector 加载每个问题独有的 xmal 文件,基于问题类型的说明和选择。
    • 如何创建一般问题的 QuestionType 和 AnswerType 部分并使用上述DataTemplate 选择从列表中的不同问题和答案类型中进行选择?加载 xaml 文件意味着加载自定义控件 (UserControls)?在这种情况下,您也可以使用它,并且 DataContext 也将被保留。我认为这会更简单,但也许我看不到整个问题:)
    • 谢谢 Rudolf...UserControls 将是我想要进入的理想场所...这让事情变得更简单,对设计师更友好...关于如何做到这一点的任何想法... .
    猜你喜欢
    • 2017-10-10
    • 1970-01-01
    • 2013-01-27
    • 1970-01-01
    • 2022-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-04
    相关资源
    最近更新 更多