Silverlight Control Toolkit TreeView无法呈现项目

时间:2009-04-24 20:14:26

标签: silverlight data-binding xaml

我正在尝试使用TreeView构建一个SL应用程序。这是我的XAML:

<UserControl xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Toolkit"
             xmlns:tkwin="clr-namespace:System.Windows;assembly=System.Windows.Controls.Toolkit"
             xmlns:basics="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"             
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="SL.MyApp.Page"
    Width="800" Height="600">
    <controls:DockPanel>        
        <controls:TreeView Name="siteTree" controls:DockPanel.Dock="Left" Width="150">
            <controls:TreeView.ItemTemplate>
                <tkwin:HierarchicalDataTemplate ItemsSource="{Binding Children}">
                    <StackPanel Orientation="Horizontal">                        
                        <TextBlock Text="{Binding Name}"/>
                    </StackPanel>
                </tkwin:HierarchicalDataTemplate>   
            </controls:TreeView.ItemTemplate>
        </controls:TreeView> 
        <basics:TabControl controls:DockPanel.Dock="Right" TabStripPlacement="Top">
            <basics:TabItem Header="Sites"></basics:TabItem>
            <basics:TabItem Header="Lists"></basics:TabItem>
            <basics:TabItem Header="Users"></basics:TabItem>
        </basics:TabControl>      
    </controls:DockPanel>
</UserControl>

相关的代码隐藏:

namespace SL.MyApp
{
    public partial class Page : UserControl
    {
        private ObservableCollection<WebDescriptor> _webHierarchy = new ObservableCollection<WebDescriptor>(); 

        public Page(WebsSvc.WebsSoapClient websClient)
        {           
            InitializeComponent();

            siteTree.ItemsSource = _webHierarchy;

            websClient.GetWebCollectionCompleted 
                += new EventHandler<SL.SiteBuilder.WebsSvc.GetWebCollectionCompletedEventArgs>(websClient_GetWebCollectionCompleted);
            websClient.GetWebCollectionAsync();

            // TODO: some kind of spinner or progress bar needs to be started.
        }

        void websClient_GetWebCollectionCompleted(object sender, SL.SiteBuilder.WebsSvc.GetWebCollectionCompletedEventArgs e)
        {
            foreach (XElement xe in e.Result.Elements())
            {
                _webHierarchy.Add(ServiceObjectParser<WebDescriptor>.Parse(xe));
            }
        }      
    }
}

更新:WebDescriptor类:

public class WebDescriptor
{
    public string Title { get; set; }
    public string Url { get; set; }
    public List<WebDescriptor> Children { get; set; }
}

我的问题是TreeView根本没有渲染。我已经验证了在websClient_GetWebCollectionCompleted中获得的结果是有效且正确的,但......没有。

有什么想法吗?

2 个答案:

答案 0 :(得分:0)

在websClient_GetWebCollectionCompleted:

siteTree.ItemsSource = _webHierarchy;

此外,将Treeview中TextBlock上的绑定更改为{Binding Title}

答案 1 :(得分:0)

在查看绑定跟踪输出后,我终于意识到我试图绑定到字段而不是属性。

卫生署!