0

I have a TreeView with HierarchicalDataTemplate that I'm trying to bind with an ObservableCollection of custom types.
But the HierarchicalDataTemplate's DataType attribute's dropdown list of available types in my Namespace is incomplete, it's missing the TFolderItem custom type, but listing all the other custom types in that same namespace. The namespace is MyProject.Classes, and classes are in plain Classes folder in the project directory.
I don't understand why it's not showing in the XAML code editor dropdown.

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

Here is the view's 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>

This is the viewmodel:

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); }
    }
}
Daniel Hilgarth
  • 171,043
  • 40
  • 335
  • 443
steakoverflow
  • 1,206
  • 2
  • 15
  • 24
  • How are we supposed to fix your code if you neither show your XAML, nor the code behing / view model? – Daniel Hilgarth Sep 10 '15 at 17:03
  • 1
    First of all, your bindings are incorrect. You have `FtpPathr` instead of `FtpPath` and `Childrenx` instead of `Children`. That the TFolderItem class is missing might simply be due to the fact that you haven't compiled your code since you added that class. – Daniel Hilgarth Sep 11 '15 at 06:16
  • Bindings are incorrect because I was trying to get any sort of Bindings debug in output console.
    I've tried clean and rebuild solution, but it didn't help.
    – steakoverflow Sep 11 '15 at 10:02
  • It seems that if I remove the constructor from TFolderItem, the x:type dropdown shows the TFolderItem as available type. Can anyone tell me why is that? – steakoverflow Sep 18 '15 at 16:13

1 Answers1

1

It seems that x:type dropdown only displays classes with default constructor. Adding one in TFolderItem class made it display in x:type dropdown.

steakoverflow
  • 1,206
  • 2
  • 15
  • 24