C# 为什么在展开“TreeViewItem”时调用命令不';不行?

C# 为什么在展开“TreeViewItem”时调用命令不';不行?,c#,wpf,mvvm,treeview,treeviewitem,C#,Wpf,Mvvm,Treeview,Treeviewitem,我试图在TreeViewItem按说明展开时调用该命令,但由于某些原因,它不起作用。我想这是因为HierarchycalDataTemplate,但我不知道为什么 有人知道问题出在哪里吗 XAML <Window x:Class="MyProject.MainWindow" ... xmlns:local="clr-namespace:MyProject" xmlns:bindTreeViewExpand="clr-namespace:MyP

我试图在
TreeViewItem
按说明展开时调用该命令,但由于某些原因,它不起作用。我想这是因为HierarchycalDataTemplate,但我不知道为什么

有人知道问题出在哪里吗

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>
视图模型

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()
        {
            ...
        }
    }
}
名称空间MyProject
{
公共类驱动文件
{
公共字符串名称{get;set;}
公共字符串Id{get;set;}
公用文件夹{get;protected set;}
公共驱动器文件()
{
IsFolder=false;
}
}
公共类驱动器文件夹:驱动器文件
{
公共列表子项{get;set;}
公用文件夹()
{
IsFolder=true;
Children=新列表();
}
}
公共类驱动视图模型
{
公共IList根文件夹
{
得到
{
返回GetRootFolders();
}
}
专用ICommand _expandingCommand;
公共ICommand ExpandingCommand
{
得到
{
如果(_expandingCommand==null)
{
_expandingCommand=新的RelayCommand(Foo);
}
返回扩展命令;
}
}
私人驾驶服务(私人驾驶服务);;
私有IList GetRootFolders()
{
...
}
}
}
a您的绑定错误。 您可以使用应用于每个
TreeViewItem
的样式定义绑定。在这个绑定中,源是每个
treevieItem
本身的
DataContext
。这将是一个
DriveFolder
DriveFile
对象

当然,这些对象没有
ExpandingCommand
属性,因此绑定失败

更改绑定时,请使用
TreeView
DataContext
作为源(以访问视图模型及其命令)。您可以使用
元素名称
相对资源
,例如:

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

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