0

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

在 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();
}

谢谢

4

2 回答 2

1

一些东西

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

你不能绑定到私人成员。

您需要使用 ObservableCollections 而不是列表。绑定管理器有效地侦听由 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; }
        }

    }

xml

 <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>
于 2013-04-09T15:56:18.613 回答
0

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

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

于 2013-04-09T17:47:23.190 回答