0

I know many threads have been opened and I all read those to help better understand how to use the treeview and still I'm unable to render the treeview properly if not at all.

I only succeed to render one level of the tree and I have 3 level.

My question is : What is wrong with my code below ?

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

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

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

In my viewmodel, I have the following line of interest which is initialized properly on modelview load and it works fine :

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

Finally, my xaml treeview code to render this observable collection :

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

1 Answer 1

1

Most likely because ItemsSource on HierarchicalTemplate must be a collection, but

public BoardTypesModel product_board_type { get; set; }

is not. I'm pretty sure the output window should display a binding error message for this.

If you expose an IEnumerable property to bind to instead I think it will work.

Sign up to request clarification or add additional context in comments.

3 Comments

There are no binding error in the output window but I will test with your IEnumerable proposal and see what it does. Thanks
it works like a charm. I haven't seen anywhere that ItemsSource must be a collection (IEnumerable or ObservableCollection)
@Mr.Bone Anytime you bind an ItemsSource, it's a collection. The documentation also mentions that it must bind to a collection: msdn.microsoft.com/en-us/library/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.