繁体   English   中英

WPF C#Treeview HierarchicalDataTemplate基本实现

[英]WPF C# Treeview HierarchicalDataTemplate Basic implementation

我知道已经打开了许多线程,并且我都阅读了这些线程,以帮助更好地理解如何使用树视图,但是即使根本没有,我也无法正确呈现树视图。

我仅成功渲染了树的一个级别,而我只有3个级别。

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

1级课程:

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级课程:

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级课程:

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

在我的viewmodel中,我感兴趣的是以下行,可以在modelview加载时正确初始化,并且可以正常工作:

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

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

如果您公开要绑定的IEnumerable属性,我想它将起作用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM