【问题标题】:Datatype dropdown in XAML designer not showing custom ClassXAML 设计器中的数据类型下拉菜单未显示自定义类
【发布时间】:2015-09-10 16:50:41
【问题描述】:

我有一个带有 HierarchicalDataTemplate 的 TreeView,我正在尝试将其与自定义类型的 ObservableCollection 绑定。
但是我的命名空间中 HierarchicalDataTemplate 的 DataType 属性的可用类型下拉列表不完整,它缺少 TFolderItem 自定义类型,但列出了同一命名空间中的所有其他自定义类型。命名空间为MyProject.Classes,类位于项目目录的普通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 也不显示代码行为/视图模型,我们应该如何修复您的代码?
  • 首先,您的绑定不正确。你有FtpPathr 而不是FtpPathChildrenx 而不是Children。缺少 TFolderItem 类可能仅仅是因为您在添加该类后尚未编译代码。
  • 绑定不正确,因为我试图在输出控制台中进行任何类型的绑定调试。
    我尝试过清理和重建解决方案,但没有帮助。
  • 看来,如果我从 TFolderItem 中删除构造函数,x:type 下拉菜单会将 TFolderItem 显示为可用类型。谁能告诉我这是为什么?

标签: c# treeview prism hierarchicaldatatemplate xaml-designer


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2014-04-30
    • 1970-01-01
    • 2014-01-06
    • 2014-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多