We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to load items in WPF HierarchyNavigator (Breadcrumb) control in on-demand?

Platform: WPF |
Control: HierarchyNavigator |
Published Date: March 17, 2020 |
Last Revised Date: March 17, 2020

This article describes how to dynamically load next level items (on-demand loading) in WPF HierarchyNavigator (Breadcrumb) when item is selected.

In this example, files in the system drive are load as next level items in on-demand using the HierarchyNavigatorSelectedItemChanged event.

The following code example demonstrates the same.

XAML

MainWindow.xaml

<Grid>
    <syncfusion:HierarchyNavigator ItemsSource = "{Binding HierarchyItems}" x:Name="hierarchy" Height="50" Width="500" HierarchyNavigatorSelectedItemChanged="Hierarchy_HierarchyNavigatorSelectedItemChanged">
        <syncfusion:HierarchyNavigator.ItemTemplate>
            <HierarchicalDataTemplate ItemsSource = "{Binding FileItems}" >
                <TextBlock Text="{Binding Content}"/>
            </HierarchicalDataTemplate>
        </syncfusion:HierarchyNavigator.ItemTemplate>
    </syncfusion:HierarchyNavigator>
</Grid>

 

In the below code, next level items are added to the collection in HierarchyNavigatorSelectedItemChanged in on-demand.

C#

MainWindow.xaml.cs

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }
 
    private void Hierarchy_HierarchyNavigatorSelectedItemChanged(object sender, Syncfusion.Windows.Tools.Controls.HierarchyNavigatorSelectedItemChangedEventArgs e)
    {
        if (hierarchy.SelectedItem == null)
            return;
        HierarchicalModel selectedItem = hierarchy.SelectedItem as HierarchicalModel;
        string[] dirs = Directory.GetDirectories(selectedItem.Content, "*", SearchOption.TopDirectoryOnly);
        foreach (string dir in dirs)
        {
            selectedItem.FileItems.Add(new HierarchicalModel(dir));
        }
    }
}

 

HierarchicalModel.cs

public class HierarchicalModel
{
    public HierarchicalModel(string content, params HierarchicalModel[] items)
    {
        Content = content;
        fileItems = new ObservableCollection<HierarchicalModel>();
        foreach (var item in items)
        {
            fileItems.Add(item);
        }
    }
 
    private ObservableCollection<HierarchicalModel> fileItems;
 
    public ObservableCollection<HierarchicalModel> FileItems
    {
        get { return fileItems; }
        set
        {
            if (fileItems != value)
            {
                fileItems = value;
            }
        }
    }
 
    public string Content { get; set; }
}

 

HierarchicalViewModel.cs

public class HierarchicalViewModel
{
    public HierarchicalViewModel()
    {
        hierarchyItems = new ObservableCollection<HierarchicalModel>();
        this.hierarchyItems.Add(
            new HierarchicalModel("My Files",
            new HierarchicalModel("D:\\"),
            new HierarchicalModel("C:\\"))
            );
    }
 
    private ObservableCollection<HierarchicalModel> hierarchyItems;
 
    public ObservableCollection<HierarchicalModel> HierarchyItems
    {
        get { return hierarchyItems; }
        set { hierarchyItems = value; }
    }
}

 

 

The output for the above code is shown below:

 

Output


 

 

 

 

 

 

 

 

 

View sample in GitHub

 

2X faster development

The ultimate WPF UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon