簡體   English   中英

為什么在展開`TreeViewItem` 時調用命令不起作用?

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

我試圖在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()
        {
            ...
        }
    }
}

一種

你的綁定是錯誤的。 您可以在適用於每個TreeViewItem的樣式中定義綁定。 在此綁定中,源是每個TreeViewItem本身的DataContext 那將是一個DriveFolder或一個DriveFile對象。

當然,這些對象沒有ExpandingCommand屬性,因此您的綁定會失敗。

以將TreeViewDataContext用作源(以訪問您的視圖模型及其命令)的方式更改您的綁定。 您可以使用ElementNameRelativeSource ,例如像這樣:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM