Search code examples
c#wpfmvvmtreeviewtreeviewitem

Why invoking command when `TreeViewItem` is expanded doesn't work?


I'm trying to invoke command when TreeViewItem is expanded as explained here, but for some reason it doesn't work. I think it's because of HierarchicalDataTemplate, but I don't know why.

Does any one have an idea what's the problem?

XAML

<Window x:Class="MyProject.MainWindow"
        ...
        xmlns:local="clr-namespace:MyProject"
        xmlns:bindTreeViewExpand="clr-namespace:MyProject"
        Title="MainWindow" Height="350" Width="525">
    <StackPanel>
        <TreeView ItemsSource="{Binding RootFolders}">
            <TreeView.Resources>
                <Style TargetType="TreeViewItem">
                    <Setter Property="bindTreeViewExpand:Behaviours.ExpandingBehaviour" Value="{Binding ExpandingCommand}"/>
                </Style>
            </TreeView.Resources>
            <TreeView.ItemTemplate>
                <HierarchicalDataTemplate ItemsSource="{Binding Children}" DataType="{x:Type local:DriveFolder}">
                    <TreeViewItem Header="{Binding Name}" />
                </HierarchicalDataTemplate>
            </TreeView.ItemTemplate>
        </TreeView>
    </StackPanel>
</Window>

BEHAVIOURS

namespace GooglePhotoPermissions
{
    public static class Behaviours
    {
        #region ExpandingBehaviour (Attached DependencyProperty)
        public static readonly DependencyProperty ExpandingBehaviourProperty =
            DependencyProperty.RegisterAttached("ExpandingBehaviour", typeof(ICommand), typeof(Behaviours),
                new PropertyMetadata(OnExpandingBehaviourChanged));

        public static void SetExpandingBehaviour(DependencyObject o, ICommand value)
        {
            o.SetValue(ExpandingBehaviourProperty, value);
        }
        public static ICommand GetExpandingBehaviour(DependencyObject o)
        {
            return (ICommand)o.GetValue(ExpandingBehaviourProperty);
        }
        private static void OnExpandingBehaviourChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            TreeViewItem tvi = d as TreeViewItem;
            if (tvi != null)
            {
                ICommand ic = e.NewValue as ICommand;
                if (ic != null)
                {
                    tvi.Expanded += (s, a) =>
                    {
                        if (ic.CanExecute(a))
                        {
                            ic.Execute(a);

                        }
                        a.Handled = true;
                    };
                }
            }
        }
        #endregion
    }
}

ViewModel

namespace MyProject
{
    public class DriveFile
    {
        public string Name { get; set; }
        public string Id { get; set; }
        public bool IsFolder { get; protected set; }

        public DriveFile()
        {
            IsFolder = false;
        }
    }

    public class DriveFolder : DriveFile
    {
        public List<DriveFile> Children { get; set; }

        public DriveFolder()
        {
            IsFolder = true;
            Children = new List<DriveFile>();
        }
    }
    public class DriveViewModel
    {
        public IList<DriveFolder> RootFolders
        {
            get
            {
                return GetRootFolders();
            }
        }

        private ICommand _expandingCommand;
        public ICommand ExpandingCommand
        {
            get
            {
                if (_expandingCommand == null)
                {
                    _expandingCommand = new RelayCommand(Foo);
                }

                return _expandingCommand;
            }
        }

        private DriveService _driveService;

        private IList<DriveFolder> GetRootFolders()
        {
            ...
        }
    }
}

a


Solution

  • Your binding is wrong. You define a binding in a style that applies to each TreeViewItem. In this binding, the source is the DataContext of each TreeViewItem itself. That would be a DriveFolder or a DriveFile object.

    Of course, these objects have no ExpandingCommand property, so your binding just fails.

    Change your binding in such a manner that the DataContext of the TreeView is used as source (to access your view model and its command). You can use ElementName or RelativeSource, e.g. like this:

    <Setter Property="bindTreeViewExpand:Behaviours.ExpandingBehaviour"
        Value="{Binding DataContext.ExpandingCommand, RelativeSource={RelativeSource AncestorType=TreeView}}"/>