1

I want to use a treeview to show a Question and an Answer. It will pull a list of questions to be answered and if they have been answered in the past it will show the answer to that question. Each answer will either be a list in a combobox or straight text. I need it to pull the correct template for the answer. I have stored in the database an id of which one of the templates that the answer should pull. I have never used this before so I am not sure how. If there is a better way I am all for it too.

<src:UEFUserControl x:Class="EmpCoverage.WorkObjects.Controls.GRWOInvestigatorQuestions"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:party="clr-namespace:Party_Info.Classes;assembly=PartyInfo"
         xmlns:src="clr-namespace:OIC.Infrastructure.Classes;assembly=OIC.Infrastructure" 
         xmlns:OIC="clr-namespace:EmpCoverage.Classes.General_Referral"            
         Width="Auto" Height="Auto">
    <UserControl.Resources>
    <Style x:Key="{x:Type TextBox}" TargetType="TextBox" BasedOn="{StaticResource {x:Type TextBox}}">
        <Setter Property="Height" Value="Auto" />
        <Setter Property="MinWidth" Value="100"/>
        <Setter Property="CharacterCasing" Value="Upper"/>
    </Style>

    <Style x:Key="{x:Type Label}" TargetType="Label" BasedOn="{StaticResource {x:Type Label}}">
        <Setter Property="HorizontalAlignment" Value="Right"/>
    </Style>

    <HierarchicalDataTemplate x:Name="hdtTextBox" DataType="{x:Type OIC:MyTemplateSelector}" ItemsSource="{Binding Path=Answers}">
        <StackPanel>
            <Label Content="{Binding Path=Question}" Grid.Row="0" Grid.Column="1" Visibility="Hidden"/>
            <TextBlock Name="txtAnswer" Text="{Binding Path=Answer}" Grid.Row="1" Grid.Column="0" TextWrapping="Wrap" />
        </StackPanel>
    </HierarchicalDataTemplate>

    <HierarchicalDataTemplate x:Name="hdtComboBox" DataType="{x:Type OIC:MyTemplateSelector}" ItemsSource="{Binding Path=Answers}">
        <StackPanel>
            <Label Content="{Binding Path=Question}" Grid.Row="0" Grid.Column="1" Visibility="Hidden"/>
            <ComboBox Name="cboAnswer" Grid.Row="1" Grid.Column="0" Width="Auto" MinWidth="200"
                      ItemsSource="{Binding Answers}" DisplayMemberPath="Answer" SelectedValue="{Binding Path=Answer}" />
        </StackPanel>
    </HierarchicalDataTemplate>

</UserControl.Resources>

<Grid Width="Auto" Height="Auto" Name="gdGeneralReview">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="40"/>
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition Width="Auto"/>
    </Grid.ColumnDefinitions>

    <TreeView Name="tvGeneralReferral" Grid.Row="0" Grid.Column="0" SelectedItemChanged="tvGeneralReferral_SelectedItemChanged" >

    </TreeView>


    <Button Grid.Row="4" Grid.Column="0" Grid.ColumnSpan="2" Name="btnSave" Height="30" Width="60" Content="Save" VerticalAlignment="Center" Click="btnSave_Click" />
</Grid>

Bryan
  • 105
  • 1
  • 4
  • 11
  • possible duplicate of [How to Dynamically Populate a treeView in WPF getting value from database?](http://stackoverflow.com/questions/13527030/how-to-dynamically-populate-a-treeview-in-wpf-getting-value-from-database) – Kritner May 11 '15 at 18:40
  • I don't think so. I need to use two different templates based on the type of answer that is possible. Whether it be a list in a combobox or a plain textbox. – Bryan May 11 '15 at 19:06
  • The DataType of the HierarchicalDataTemplate needs to refer to the type of the item in the treeview's ItemSource, not the type of the control you want to display. You can use my answer below to return those data templates. – Sean Beanland May 11 '15 at 19:32

1 Answers1

1

You can define an ItemTemplateSelector that will return the proper template based on logic you provide.

<Window.Resources>
    <vm:MyTemplateSelector x:Key="MyTemplateSelector" />
</Window.Resources>

<TreeView ItemTemplateSelector={StaticResource MyTemplateSelector} />

A sample selector

public class MyTemplateSelector : DataTemplateSelector
{
    public override DataTemplate SelectTemplate(object item, DependencyObject container)
    {
        if (item is string)
            return ((FrameworkElement) container).FindResource("TextTemplate") as DataTemplate;
        else
            return ((FrameworkElement)) container).FindResource("EnumTemplate") as DataTemplate;             
    }
}
Sean Beanland
  • 1,098
  • 1
  • 10
  • 25