Stéphan F
Stéphan F

Reputation: 95

Caliburn.Micro TreeView add a static element

I have a working TreeView

                        <TreeView x:Name="TVAccess" ItemsSource="{Binding AccessLevel}" Grid.Row="1" Grid.Column="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.ColumnSpan="3">
                        <TreeView.ItemTemplate>
                            <HierarchicalDataTemplate  >
                                <StackPanel Orientation="Horizontal">
                                    <!--<CheckBox Checked="{Binding Checked}"/>-->
                                    <TextBlock Text="{Binding Text}" />
                                </StackPanel>
                            </HierarchicalDataTemplate>
                        </TreeView.ItemTemplate>
                    </TreeView>

It is bind to AccessLevel object defined like this

public BindableCollection<UserLibrary.DataAccess.TextHelper.TreeViewItem> AccessLevel { get; set; }

This works well The result is this

enter image description here

I would like to add a fixed first level named "Access Right", how can I do that?

EDIT 1

The structure I would like is this:

enter image description here

Thank you for your help.

Edit 2 - The TreeViewItem class

public class TreeViewItem
{
    public string Text { get;  set; }
    public bool Checked { get;  set; }
}

Edit 3 - Modify the TreeViewItem Class

So if I mofify my call like this, now the problem is to bind it to the TreeView

    public class TreeViewItem
{
    public string Text { get;  set; }
    public bool Checked { get; set; }

    public IEnumerable<TreeViewItem> SubTreeViewItem { get; set; } 
}

Upvotes: 0

Views: 109

Answers (1)

mm8
mm8

Reputation: 169190

Insert a TreeViewItem to the source collection at index 0.

AccessLevel.Insert(0, new UserLibrary.DataAccess.TextHelper.TreeViewItem() { Text = "Access Right" } );

Or set the ItemsSource property to a CompositeCollection and define the fixed item in the XAML markup:

<TreeView x:Name="TVAccess" Grid.Row="1" Grid.Column="0" 
                  HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.ColumnSpan="3">
    <TreeView.Resources>
        <CollectionViewSource x:Key="source" Source="{Binding AccessLevel}" />
    </TreeView.Resources>
    <TreeView.ItemsSource>
        <CompositeCollection>
            <local:TreeViewItem Text="Access Right" />
            <CollectionContainer Collection="{Binding Source={StaticResource source}}" />
        </CompositeCollection>
    </TreeView.ItemsSource>
    <TreeView.ItemTemplate>
        <HierarchicalDataTemplate  >
            <StackPanel Orientation="Horizontal">
                <!--<CheckBox Checked="{Binding Checked}"/>-->
                <TextBlock Text="{Binding Text}" />
            </StackPanel>
        </HierarchicalDataTemplate>
    </TreeView.ItemTemplate>
</TreeView>

Upvotes: 1

Related Questions