Reputation: 2559
Hello I am working with a WPF
application where I am wanting to have an event fire before I expand an item on a TreeView
. I know that there isn't an event already for this but I am not sure how to add one the way that I am doing this so any help will be appreciated.
here is the xaml.
<TreeView x:Name="TreeView" Margin="20">
<TreeView.Resources>
<Style TargetType="{x:Type TreeViewItem}">
<!-- Some style -->
</Style>
<DataTemplate DataType="{x:Type models:DirectoryPathItem}">
<!-- some template -->
</DataTemplate>
</TreeView.Resources>
</TreeView>
I am loading in code behind.
private void LoadBaseDirectories()
{
var directoryInfo = new DirectoryInfo(@"C:\");
var directories = directoryInfo.GetDirectories();
foreach (var directory in directories)
{
TreeView.Items.Add(new DirectoryPathItem()
{
Children = new ObservableCollection<PathItem>(),
Path = directory.FullName,
Header = directory.Name
});
}
}
Is there a way that I can call an event before expanding the TreeViewItem
so I can load the children?
Upvotes: 2
Views: 287
Reputation: 2718
Loading the children items before you actually expand may result in an unresponsive UI where the user does not understand why the list is empty.
I would suggest adding an item which represent the background task (i.g.: a loading spinner of some sort) and add proper items as soon as they are discovered.
Once done you may remove the loading spinner item.
This way, you won't need a new event and the user will understand what is going on.
Upvotes: 1