c# - 为什么在扩展 `TreeViewItem` 时调用命令不起作用?

标签 c# wpf mvvm treeview treeviewitem

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

有谁知道有什么问题?

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 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
    }
}

查看型号
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()
        {
            ...
        }
    }
}

一种

最佳答案

你的绑定(bind)是错误的。
您以适用于每个 TreeViewItem 的样式定义绑定(bind)。 .在此绑定(bind)中,源是 DataContext每个 TreeViewItem本身。那将是 DriveFolderDriveFile目的。

当然,这些对象没有ExpandingCommand属性,所以你的绑定(bind)失败了。

以这样的方式更改您的绑定(bind),DataContextTreeView用作源(访问您的 View 模型及其命令)。您可以使用 ElementNameRelativeSource ,例如像这样:

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

关于c# - 为什么在扩展 `TreeViewItem` 时调用命令不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41734206/

相关文章:

wpf - 在不使用 INotifyPropertyChange 的情况下从头开始实现 MVVM?

c# - 标签中未显示 Unicode 特殊字符

c# - 是否可以将数据从 AngularJS 传递到 Web 服务中带有模型参数的函数?

c# - 将 ASP.NET MVC 5 项目迁移到 ASP.NET 5

wpf - 使用 wpf datagridcomboboxcolumn 的 IsSynchronizedWithCurrentItem

wpf - 如何让 ListView(或 DataGrid)与 TextWrapping (WPF) 一起使用

ios - 使用两种类型的单元实现 MVVC 以进行向下钻取 UITableview

wpf - 在 View 模型中拥有 View 模型是不是很糟糕?

c# - 在 C# 中使用 HttpClient 提交表单

wpf - 在 WPF DataGrid 为空时显示 "No record found"消息