XAML设计器中的数据类型下拉列表没有显示自定义类

本文关键字:显示 自定义 下拉列表 数据类型 XAML | 更新日期: 2023-09-27 18:12:06

我有一个TreeView与HierarchicalDataTemplate,我试图绑定自定义类型的ObservableCollection。
但是HierarchicalDataTemplate的DataType属性的可用类型下拉列表在我的命名空间是不完整的,它缺少TFolderItem自定义类型,但在同一命名空间中列出所有其他自定义类型。命名空间为MyProject.Classes,类在项目目录的plain classes文件夹下。
我不明白为什么它没有显示在XAML代码编辑器下拉列表中。

public class TFolderItem
{
    /*public FolderItem(RemoteDirectoryInfo rdi, WinSCP.Session winscpSession)
    {
        RDI = rdi;
        this.WinSCPSession = winscpSession;
    }*/
    public TFolderItem(string path, WinSCP.Session winscpSession)
    {
        RDI = winscpSession.ListDirectory(path);
        this.FtpPath = path;
        this.WinSCPSession = winscpSession;
    }
    private WinSCP.Session winscpSession;
    public RemoteDirectoryInfo RDI { get; set; }
    public string FtpPath { get; set; }
    public WinSCP.Session WinSCPSession
    {
        get { return this.winscpSession; }
        set { this.winscpSession = value; }
    }
    public IList Children
    {
        get
        {
            var children = new CompositeCollection();
            var subDirItems = new List<TFolderItem>();
            var subDirFiles = new List<RemoteFileInfo>();
            foreach (RemoteFileInfo rfi in RDI.Files)
            {
                if (rfi.IsDirectory)
                {
                    subDirItems.Add(new TFolderItem(this.FtpPath + rfi.Name + "/", this.WinSCPSession));
                }
                else
                {
                    subDirFiles.Add(rfi);
                }
            }
            children.Add(new CollectionContainer
            {
                Collection = subDirItems
            });
            children.Add(new CollectionContainer
            {
                Collection = subDirFiles
            });
            return Children;
        }
    }
}

下面是视图的xaml:

<UserControl x:Class="MyProject2.Views.FTPTab"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:MyProject2.Views"             
         xmlns:MyProject2Classes="clr-namespace:MyProject2.Classes"                 

         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
<Grid>
    <TreeView  ItemsSource="{Binding FolderItems}" Height="300" Width="300">
        <TreeView.Resources >
            <HierarchicalDataTemplate DataType="" ItemsSource="{Binding Childrenx}">
                <TextBlock Text="{Binding FtpPathr}"/>
            </HierarchicalDataTemplate>
            <DataTemplate DataType=":">
                <TextBlock Text="{Binding Name}" />
            </DataTemplate>
        </TreeView.Resources>
    </TreeView>
</Grid>

这是视图模型:

public class FTPTabViewModel : BindableBase
{
    public FTPTabViewModel(string host, WinSCP.Session winscpSession)
    {
        this.Host = host;
        this.FolderItems = new ObservableCollection<TFolderItem>();
        this.Session = winscpSession;            
        this.FolderItems.Add(new TFolderItem("/",Session));
    }
    private WinSCP.Session session;
    private ObservableCollection<TFolderItem> folderItems;
    private string host;
    public string Host
    {
        get { return this.host; }
        set { this.host = value; }
    }
    public WinSCP.Session Session
    {
        get { return session; }
        set { this.session = value; }
    }
    public ObservableCollection<TFolderItem> FolderItems
    {
        get { return folderItems; }
        set { SetProperty(ref this.folderItems, value); }
    }
}

XAML设计器中的数据类型下拉列表没有显示自定义类

似乎x:type下拉菜单只显示具有默认构造函数的类。在TFolderItem类中添加一个使其显示在x:type下拉列表中