gpt4 book ai didi

c# - 为 ListBox 中的每个项目加载不同的 DataTemplate

转载 作者:太空宇宙 更新时间:2023-11-03 10:52:02 25 4
gpt4 key购买 nike

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

     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

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

learn page

谁能帮我怎么做。

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

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>

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

最佳答案

如果您创建从通用 Quiestion ViewModel 派生的 ViewModel-s,则可以创建列表 (ObservableCollection<Question>)。然后使用以下列表框:

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

我认为这应该可以为您完成工作。

关于c# - 为 ListBox 中的每个项目加载不同的 DataTemplate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21111817/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com