【问题标题】:Why invoking command when `TreeViewItem` is expanded doesn't work?为什么在展开“TreeViewItem”时调用命令不起作用?
【发布时间】:2017-01-19 05:12:15
【问题描述】:

我试图在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()
        {
            ...
        }
    }
}

一个

【问题讨论】:

    标签: c# wpf mvvm treeview treeviewitem


    【解决方案1】:

    您的绑定错误。 您以适用于每个TreeViewItem 的样式定义绑定。在此绑定中,源是每个TreeViewItem 本身的DataContext。那将是 DriveFolderDriveFile 对象。

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

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

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

    【讨论】:

      猜你喜欢
      • 2014-06-12
      • 2018-07-07
      • 1970-01-01
      • 2019-06-27
      • 2013-01-14
      • 1970-01-01
      • 2013-09-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多