2014-01-14 4 views
2

학습용 애플리케이션을 만들려고하고 있으며 아래 설명 된 질문 유형 을 기반으로 데이터 템플릿을로드하고 싶습니다. ListBox의 각 항목에 대해 다른 DataTemplate로드하기

 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

는 사람이 어떻게이 작업을 수행하는 데 도움이 있습니다.

어떤 사람이 나를 도울 수

<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> 

... 내 이전 게시물

Nested ObservableCollection data binding in WPF

의 코드를 사용하고와 XAML입니다 이것이 어떻게 될 수 있는지 이해함 똑똑한 디자인 (질문에 대한 일반적인 기본 클래스 수 있으며 각 질문 유형에 대한 파생 된 질문 클래스 및 클래스에서 가상 함수를 사용하여 데이터 템플릿을로드 ...)하지만이 템플릿을 사용하여 수행 할 수 있습니다 궁금합니다 Selector ... 아니면 다른 접근법을 사용해야합니까?

답변

1

일반적인 Quiestion ViewModel에서 파생 된 ViewModel을 만드는 경우 목록 (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이 될 것입니다 : 그런 다음 목록 상자를 사용합니다. 당신은 ViewModels에 대한 네임 스페이스 VMs 경우 (XAML 파일의 상단에 귀하의 ViewModels (예 : xmlns:VM="clr-namespace:YourApp.VMs")에있는 네임 스페이스에 대한 참조를 추가해야합니다.

내가이 당신을 위해 일을한다고 생각합니다.

+0

감사합니다. Rudolf.The 문제는 내가 지시와 선택을위한 별도의 템플릿을 원한다는 것입니다.이 경우에는 ItemTemplateSelector.I가 필요하다고 생각합니다. ItemTemplateSelector가 각 질문, 지시 및 선택에 따라 고유 한 xmal 파일을로드하기를 원합니다. 질문 유형 – Ven

+0

일반 질문의 QuestionType과 AnswerType 부분을 생성하고 목록에서 다른 질문과 답변 유형을 선택하기 위해 위에서 설명한 'DataTemplate'선택을 사용하는 방법은 무엇입니까? 사용자 정의 컨트롤 ('UserControls')? 이 경우에도 사용할 수 있으며 DataContext도 유지됩니다. 그것은 내가 생각하기에 더 간단 할 것이다. 그러나 어쩌면 나는 전체적으로이 문제를 볼 수 없을 것이다 :) – Rudolfking

+0

Rudolf ... UserControls는 내가 원했던 이상적인 장소가 될 것이다 ... 그것은 물건을 더 간단하고 더 디자이너 친화적으로 만든다. ... 이것을하는 방법에 대한 생각. – Ven

관련 문제