无法获取Silverlight HierarchicalDataTemplate以显示IsolatedStorage文件夹

时间:2013-04-09 15:28:47

标签: c# silverlight-4.0 treeview hierarchicaldatatemplate

这是我第一次涉及分层数据,并且遇到了一些问题。

在Silverlight 4中,我试图获取要在TreeView中显示的独立存储文件夹列表。什么都没有显示出来。我的Treeview完全是空白的。我错过了什么?我正在获取数据,这是正确的。

任何帮助都将不胜感激。

XAML

 <sdk:TreeView x:Name="FolderTreeView" Grid.Column="0" Margin="0,0,3,0" ItemsSource="{Binding _Folders}">
            <sdk:TreeView.ItemTemplate>
                <sdk:HierarchicalDataTemplate ItemsSource="{Binding Path=Folders}">
                    <TextBlock Margin="0" Text="{Binding Name, Mode=OneWay}"/>
                </sdk:HierarchicalDataTemplate>
            </sdk:TreeView.ItemTemplate>
        </sdk:TreeView>

CS

internal class Folder
{
    public Folder() { Folders = new List<Folder>(); }
    public string Name { get; set; }
    public List<Folder> Folders { get; set; }
}

private List<Folder>  _Folders = new List<Folder>();
public OpenFileDialog()
{
    InitializeComponent();

    ifs = IsolatedStorageFile.GetUserStoreForApplication();
    var folder = new Folder
    {
        Name = "Root",
        Folders = (from c in ifs.GetDirectoryNames()
            select new Folder
            {
                Name = c,
                Folders = LoadFolders(c)
            }).ToList()
    };
    _Folders.Add(folder);
    FolderTreeView.DataContext = new { _Folders };
}

private List<Folder>LoadFolders(string folderName)
{
    if(folderName == null)
        return null;
    return (from c in ifs.GetDirectoryNames(folderName + "\\*.*")
        select new Folder
        {
            Name = c,
            Folders = LoadFolders(c)
        }).ToList();
}

由于

2 个答案:

答案 0 :(得分:1)

一些事情

<sdk:TreeView x:Name="FolderTreeView" Grid.Column="0" Margin="0,0,3,0" 
              ItemsSource="{Binding _Folders}">

您无法绑定到私人成员。

您需要使用ObservableCollections而不是Lists。绑定管理器有效地侦听ObservableCollection触发的CollectionChanged事件,并通知绑定的控件。

您需要在属性设置器中实现INotifyPropertyChanged并引发PropertyChanged通知。

最后,您是否为Treeview设置了DataContext?

另外,请在输出调试窗口中查看与绑定相关的错误。

编辑,好的尝试:

FolderTreeView.DataContext =  this;

并将_Folders包装在属性

public ObservableCollection <Folder> Folders
    {
    get
       {
       return _Folders;
       }
    set
       {
       _Folders = value;
       OnPropertyChanged("Folders");
       }
   }

将您的绑定更改为

<sdk:TreeView x:Name="FolderTreeView" Grid.Column="0" Margin="0,0,3,0" 
              ItemsSource="{Binding Folders}">

我已经改变了很多东西,

    public class Folder : INotifyPropertyChanged
        {

        public Folder(string folderName) 
            {
            Name = folderName;
            Folders = new ObservableCollection<Folder>();
            var _ifs = IsolatedStorageFile.GetUserStoreForApplication();

            if (folderName != null)
                {
                Folders = new ObservableCollection<Folder>(
                        (from c in _ifs.GetDirectoryNames(folderName + "\\*.*")
                         select new Folder(folderName + "\\" + c)
                   ));
                }
            else
                {
                Folders = new ObservableCollection<Folder>(
                       (from c in _ifs.GetDirectoryNames()
                        select new Folder(folderName + "\\" + c)
                  ));
                }
            }


        public string Name { get; set; }

        private ObservableCollection<Folder> _Folders; 
        public ObservableCollection<Folder> Folders 
            {
            get { return _Folders; }
            set { _Folders = value; OnPropertyChanged("RootFolder"); }
            }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged(string propertyName)
            {
            if (PropertyChanged != null)
                {
                PropertyChanged(this,
                    new PropertyChangedEventArgs(propertyName));
                }
            }
        #endregion
    }



public partial class OpenFileDialog : UserControl
    {      
    public OpenFileDialog()
        {
        InitializeComponent();

        RootFolder = new Folder (null);
        RootFolders = new ObservableCollection<Folder>();
        RootFolders.Add(RootFolder);

        FolderTreeView.DataContext = this;
        }


    private Folder _RootFolder;
    public Folder RootFolder
        {
        get { return _RootFolder; }
        set { _RootFolder = value; }
        }


    private ObservableCollection<Folder> _RootFolders;
    public ObservableCollection<Folder> RootFolders
        {
        get { return _RootFolders; }
        set { _RootFolders = value; }
        }

    }

XAML

 <sdk:TreeView x:Name="FolderTreeView"  Margin="0,0,3,0" ItemsSource="{Binding RootFolders}">
    <sdk:TreeView.ItemTemplate>
        <sdk:HierarchicalDataTemplate ItemsSource="{Binding Path=Folders}">
            <StackPanel>
            <TextBlock Margin="0" Text="{Binding Name, Mode=OneWay}"/>
            </StackPanel>
        </sdk:HierarchicalDataTemplate>
    </sdk:TreeView.ItemTemplate>
</sdk:TreeView>

答案 1 :(得分:0)

好的......问题解决了。不明白为什么。

我将ChildWindow设置为内部范围而不是公共范围,因为我不希望在Silverlight类库外部查看窗口本身。如果有人能够回答为什么这会阻止分层数据绑定而不是标准数据绑定我想知道。