Bryan
Bryan

Reputation: 105

WPF C# Treeview with dynamic list from database

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>

Upvotes: 1

Views: 652

Answers (1)

Sean Beanland
Sean Beanland

Reputation: 1118

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

Upvotes: 1

Related Questions