Reputation: 95
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
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:
Thank you for your help.
public class TreeViewItem
{
public string Text { get; set; }
public bool Checked { get; set; }
}
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
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