2012-03-13 56 views
2

我有MyPOCO对象MyCollection(它有两个字符串属性)。Silverlight Treeview inline HierarchicalDataTemplate绑定问题

当我尝试将HierarchicalDataTemplate实现到树视图中时,绑定不起作用,我得到类名。

我知道如果我从控制中取出数据模板一切正常,但我有兴趣看看为什么这个例子不起作用。

<Grid x:Name="LayoutRoot" Background="White" DataContext="{Binding Source={StaticResource testVM}}"> 
    <sdk:TreeView Margin="8,8,8,111" ItemsSource="{Binding MyCollection}"> 
     <sdk:HierarchicalDataTemplate ItemsSource="{Binding MyPOCO}"> 
      <sdk:HierarchicalDataTemplate.ItemTemplate> 
       <DataTemplate> 
        <StackPanel> 
         <TextBlock Text="{Binding Property1}"/> 
         <TextBlock Text="{Binding Property2}"/> 
        </StackPanel> 
       </DataTemplate> 
      </sdk:HierarchicalDataTemplate.ItemTemplate> 
     </sdk:HierarchicalDataTemplate> 
    </sdk:TreeView> 
</Grid> 

这里是ViewModel也。

namespace MyPOCProject 

{ 公共类MyPOCO { 私人字符串property1;

public string Property1 
    { 
     get { return property1; } 
     set { property1 = value; } 
    } 

    private string property2; 

    public string Property2 
    { 
     get { return property2; } 
     set { property2 = value; } 
    } 

    public MyPOCO(string p1, string p2) 
    { 
     property1 = p1; 
     property2 = p2; 
    } 
} 

public class MyViewModel : INotifyPropertyChanged 
{ 
    private ObservableCollection<MyPOCO> myCollection; 

    public ObservableCollection<MyPOCO> MyCollection 
    { 
     get { return myCollection; } 
     set { myCollection = value; RaisePropertyChanged("MyCollection"); } 
    } 

    public MyViewModel() 
    { 
     MyPOCO _poco1 = new MyPOCO("aaa1", "bbb1"); 
     MyPOCO _poco2 = new MyPOCO("aaa2", "bbb2"); 
     MyPOCO _poco3 = new MyPOCO("aaa3", "bbb3"); 

     MyCollection = new ObservableCollection<MyPOCO>() { _poco1, _poco2, _poco3 }; 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 

    private void RaisePropertyChanged(string propertyName) 
    { 
     if (this.PropertyChanged != null) 
     { 
      this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 
} 

}

那我做错了吗?

重新...我对这个特殊的例子感兴趣。我想知道这个例子有什么问题,为什么。

谢谢。

+0

你有更多的代码?我想看看课程。 – Silvermind 2012-03-13 18:24:42

+0

我更新了帖子。谢谢Silvermind。 – asuciu 2012-03-13 23:15:08

回答

2

您发布的代码不是分层的,换句话说:MyPOCO类不包含属性MyCollection<MYPOCO>儿童。 下面是HierarchicalDataTemplate

的XAML的例子:

<sdk:TreeView x:Name="MyTreeView" 
       HorizontalAlignment="Left" 
       ItemsSource="{Binding MyCollection}" 
       Width="200" Height="280"> 
    <sdk:TreeView.ItemTemplate> 
     <sdk:HierarchicalDataTemplate ItemsSource="{Binding Path=Children}"> 
      <TextBlock Text="{Binding Path=Header}"/> 
      <sdk:HierarchicalDataTemplate.ItemTemplate> 
       <sdk:HierarchicalDataTemplate ItemsSource="{Binding Path=Children}"> 
        <TextBlock Text="{Binding Path=Header}"/> 
       </sdk:HierarchicalDataTemplate> 
      </sdk:HierarchicalDataTemplate.ItemTemplate> 
     </sdk:HierarchicalDataTemplate> 
    </sdk:TreeView.ItemTemplate> 
</sdk:TreeView> 

代码隐藏类:

public class MyPoco 
{ 
    public MyPoco(string header, int sampleChildrenCount) 
    { 
     this.Header = header; 
     this.Children = new ObservableCollection<MyPoco>(); 
     if (sampleChildrenCount > 0) 
     { 
      for (int i = 0; i < sampleChildrenCount; i++) 
      { 
       string newHeader = String.Format("Test {0}", sampleChildrenCount * i); 
       var myPoco = new MyPoco(newHeader, sampleChildrenCount - 1) 
       this.Children.Add(myPoco); 
      } 
     } 
    } 
    public string Header { get; set; } 
    public ObservableCollection<MyPoco> Children { get; set; } 
} 

public class MyViewModel 
{ 
    public MyViewModel() 
    { 
     MyCollection = new ObservableCollection<MyPoco>(); 
     for (int i = 0; i < 6; i++) 
     { 
      this.MyCollection.Add(new MyPoco(String.Format("Test {0}", i), 5)); 
     } 
    } 
    public ObservableCollection<MyPoco> MyCollection { get; set; } 
} 

代码隐藏启动:

public MainPage() 
{ 
    public MainPage() 
    { 
     InitializeComponent(); 
     MyTreeView.DataContext = new MyViewModel(); 
    } 
} 
+0

这个想法是基于从WebService返回的任何内容,我应该能够在TreeView中显示它。想象一下当你的根目录只有几个目录时。因为当我使用模板时,相同的代码起作用,所以我真的怀疑我必须像提到的那样创建对象结构。我想这个问题来自于控件以及它附加模板资源与控件中指定的内联模板的方式。感谢您的回复。 – asuciu 2012-03-15 01:46:44

+0

但是如果代码不是分级的,它将不能在这样的HierarchicalDataTemplate中工作。即使你没有填充任何孩子,也要尽量让它分层次。否则,你应该使用DataTemplate,它是用于非分层数据的。 – Silvermind 2012-03-15 11:33:31

+0

谢谢Silverlmind。 – asuciu 2012-03-15 15:08:46