WPF TreeView不显示子项

时间:2017-03-05 18:50:08

标签: c# wpf treeview

我正在实现自定义设计的FolderSelector,并且我使用TreeView来显示文件夹。

这是观点:

<TreeView Grid.Row="1" Background="Transparent" ItemsSource="{Binding Path=Items}">
    <TreeView.ItemContainerStyle>
        <Style TargetType="{x:Type TreeViewItem}">
            <Setter Property="IsSelected" Value="{Binding Path=IsSelected}" />
        </Style>
    </TreeView.ItemContainerStyle>

    <TreeView.Resources>
        <HierarchicalDataTemplate DataType="viewModel:FolderViewModel" ItemsSource="{Binding Path=Items}">
            <DockPanel>
                <fa:FontAwesome Icon="FolderOutline" Foreground="#dd5d18" VerticalAlignment="Center" />
                <Label Content="{Binding}" VerticalAlignment="Center" FontWeight="SemiBold" />
            </DockPanel>
        </HierarchicalDataTemplate>
    </TreeView.Resources>
</TreeView>

这是整个对话框的视图模型

internal class FolderBrowserViewModel : BaseBAObject
{
    #region Fields        

    #endregion

    #region Properties

    public ObservableCollection<FolderViewModel> Items { get; }

    #endregion

    #region Construction

    public FolderBrowserViewModel()
    {
        Items = new ObservableCollection<FolderViewModel>();
        LoadItems();
    }

    #endregion

    #region Methods

    private void LoadItems()
    {
        try
        {
            var drives = DriveInfo.GetDrives();

            foreach (var drive in drives)
            {
                Items.Add(new FolderViewModel(drive.Name.Replace(@":\", ""), drive.Name));
            }
        }
        catch (Exception)
        {
            // Ignore
        }
    }
    #endregion
}

这是每个文件夹的viewmodel

internal class FolderViewModel : BaseBAObject
{
    #region Fields
    private bool _isSelected;
    private bool _isLoaded;
    #endregion

    #region Properties

    public string Path { get; }
    public string Name { get; }
    public ObservableCollection<FolderViewModel> Items { get; }

    public bool IsSelected
    {
        get { return _isSelected; }
        set
        {
            if (value != _isSelected)
            {
                _isSelected = value;
                OnPropertyChanged(nameof(IsSelected));

                LoadSubfolders();
            }
        }
    }

    #endregion

    #region Construction

    public FolderViewModel(string name, string path)
    {
        Path = path;
        Name = name;
        Items = new ObservableCollection<FolderViewModel>();
    }

    #endregion

    #region Methods

    public void LoadSubfolders(bool force = false)
    {
        if(_isLoaded && !force)
            return;

        try
        {
            var dirs = Directory.GetDirectories(Path);

            Items.Clear();
            foreach (var dir in dirs)
            {
                Items.Add(new FolderViewModel(dir, System.IO.Path.Combine(Path, dir)));
            }

            _isLoaded = true;
        }
        catch (Exception)
        {
            // ignore
        }
    }

    public override string ToString()
    {
        return Name;
    }

    #endregion
}

我现在面临几个问题

  • 数据模板不适用于TreeView根对象,也不适用于子项
  • 未显示子项(选择根项目时它们已正确加载)
  • 我想展示无限数量的孩子

1 个答案:

答案 0 :(得分:0)

明确说明类型参数,即使用{x:Type},并将Content的{​​{1}}属性绑定到Label的{​​{1}}属性:

Name