WPF C# Treeview HierarchicalDataTemplate 基本实现

WPF C# Treeview HierarchicalDataTemplate Basic implementation

我知道已经打开了很多线程,我都阅读了这些线程以帮助更好地理解如何使用树视图,但我仍然无法正确渲染树视图(如果根本不渲染的话)。

我只成功渲染了一层树,我有3层。

我的问题是:我下面的代码有什么问题?

1 级 class:

public class ProductModel
{
    public decimal? id { get; set; }
    public string product_pn { get; set; }
    public string product_desc { get; set; }
    public BoardTypesModel product_board_type { get; set; }
}

2 级 class:

public class BoardTypesModel
{
    public decimal? id { get; set; }
    public string board_type { get; set; }
    public string product_family { get; set; }
    public float board_length_inches { get; set; }
    public List<PulseCurrentModel> lsPulseCurrents { get; set; }
}

3 级 class:

public class PulseCurrentModel
{
    public decimal? id { get; set; }
    public float voltage_setpoint { get; set; }
    public float nominal_current { get; set; }
    public float current_tolerance { get; set; }
    public float nominal_power { get; set; }
    public float power_tolerance { get; set; }
    public string test_type { get; set; }
    public int order_priority { get; set; }
    public decimal? board_type_id { get; set; }
}

在我的视图模型中,我有以下感兴趣的行,它在模型视图加载时正确初始化并且工作正常:

    private ObservableCollection<Models.ProductModel> _ocProducts;
    public ObservableCollection<Models.ProductModel> ocProducts
    {
        get { return _ocProducts; }
        set
        {
            _ocProducts = value;
            RaisePropertyChangedEvent("ocProducts");
        }
    }

最后,我的 xaml 树视图代码呈现这个可观察的集合:

<TreeView DockPanel.Dock="Top" ItemsSource="{Binding ocProducts}">
    <TreeView.ItemTemplate>
        <HierarchicalDataTemplate ItemsSource="{Binding product_board_type}">
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding product_pn}" />
                <TextBlock Text=" - " Foreground="Blue" />
                <TextBlock Text="{Binding product_desc}" Foreground="Blue" />
            </StackPanel>
            <HierarchicalDataTemplate.ItemTemplate>
                <HierarchicalDataTemplate ItemsSource="{Binding lsPulseCurrents}">
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding board_type}" />
                        <TextBlock Text=" (" Foreground="Green" />
                        <TextBlock Text="{Binding board_length_inches}" Foreground="Green" />
                        <TextBlock Text=" inches) " Foreground="Green" />
                        <TextBlock Text="{Binding product_family}" Foreground="Green" />
                    </StackPanel>
                    <!--<HierarchicalDataTemplate.ItemTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal">
                                <TextBlock Text="Voltage : "></TextBlock>
                                <TextBlock Text="{Binding voltage_setpoint}" />
                            </StackPanel>
                        </DataTemplate>
                    </HierarchicalDataTemplate.ItemTemplate>-->
                </HierarchicalDataTemplate>
            </HierarchicalDataTemplate.ItemTemplate>
        </HierarchicalDataTemplate>
    </TreeView.ItemTemplate>
</TreeView>

很可能是因为 HierarchicalTemplate 上的 ItemsSource 必须是一个集合,但是

public BoardTypesModel product_board_type { get; set; }

不是。我很确定输出 window 应该为此显示一条绑定错误消息。

如果你公开一个 IEnumerable 属性 来绑定,我认为它会起作用。