WPF C# Treeview 与来自数据库的动态列表

WPF C# Treeview with dynamic list from database

我想使用树视图来显示问题和答案。它将拉出要回答的问题列表,如果过去已经回答过,它将显示该问题的答案。每个答案要么是组合框中的列表,要么是纯文本。我需要它来提取正确的答案模板。我已经在数据库中存储了答案应该提取的模板之一的 ID。我以前从未使用过它,所以我不确定如何使用。如果有更好的方法我也支持

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

您可以定义一个 ItemTemplateSelector,它将 return 基于您提供的逻辑的正确模板。

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

<TreeView ItemTemplateSelector={StaticResource MyTemplateSelector} />

样本选择器

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