user9500542
user9500542

Reputation:

Why TreeView Control doesn't binding

Here is my model:

public class TreeItem : INotifyPropertyChanged
{
    private ObservableCollection<TreeItem> _items;
    private string _fullPath;
    private string _name;

    public string Name
    {
        get
        {
            return _name;
        }
        set
        {
            _name = value;
            OnPropertyChanged("Name");
        }
    }

    public string FullPath
    {
        get
        {
            return _fullPath;
        }
        set
        {
            _fullPath = value;
            OnPropertyChanged("FullPath");
        }
    }

    public ObservableCollection<TreeItem> Items
    {
        get
        {
            return _items;
        }
        set
        {
            _items = value;
            OnPropertyChanged("Items");
        }
    }

    public TreeItem(string name)
    {
        Name = name;
        Items = new ObservableCollection<TreeItem>();
    }

    public TreeItem()
    {
        Items = new ObservableCollection<TreeItem>();
    }


    public event PropertyChangedEventHandler PropertyChanged;
    public void OnPropertyChanged([CallerMemberName]string prop = "")
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(prop));
    }
}

I implement INotifyPropertyChanged, because I think, that problem is resolved, by adding this implementation. So there is my Xaml TreeView:

<TreeView Name="FileFolderTree" Grid.Column="2" Grid.Row="2" Background="DarkGray" ItemsSource="{Binding FolderFiles}">
    <TreeView.ItemTemplate>
        <HierarchicalDataTemplate DataType="{x:Type model:TreeItem}" ItemsSource="{Binding Path=Items}">
            <TextBlock Text="{Binding Name}" />
        </HierarchicalDataTemplate>
    </TreeView.ItemTemplate>
</TreeView>

this is MainWindow.cs with DataContext equals new MainWindowViewModel:

public MainWindow()
{
    InitializeComponent();
    DataContext = new MainWindowViewModel();
}

SO, MainWindowViewModel has a public property:

private  TreeItem _folderFiles;

public TreeItem FolderFiles {
    get
    {
        return _folderFiles;
    }
    set
    {
        _folderFiles = value;
        OnPropertyChanged("FolderFiles");
    }
}

In some method, I see that _folderFiles contain data, but it doesn't show on treeView. If I will hardcode some elements in treeView, they will be shown.

What do I do not correctly?

Upvotes: 0

Views: 61

Answers (1)

dymanoid
dymanoid

Reputation: 15227

Your issue is here:

public TreeItem FolderFiles 
{
    get
    {
        return _folderFiles;
    }
    set
    {
        _folderFiles = value;
        OnPropertyChanged("FolderFiles");
    }
}

This property is of type TreeItem, but you're trying to use it in a binding for the TreeView.ItemsSource property. That property expects an IEnumerable object. If you look into your Debug Output window, you will discover a Data Error there, because the binding is unable to provide a correct value.

Change your property to an IEnumerable<TreeItem>.

Create an ObservableCollection<TreeItem> as a root container:

public ObservableCollection<TreeItem> FolderFiles { get; } = new ObservableCollection<TreeItem>();

//...

FolderFiles.Add(_folderFiles);

If you only want to have a single root element, you can do something like:

private TreeItem _folderFiles;
public IEnumerable<TreeItem> FolderFiles => new[] { _folderFiles };

(Note that this sample has no support of INotifyPropertyChanged and INotifyCollectionChanged, but you surely can implement it.)

Upvotes: 2

Related Questions