我想將自定義類型的ObservableCollection綁定到TreeView。Binding ObservableCollection <MyClass> to TreeView
定製ErrorDetails類看起來是這樣的:
class ErrorDetail
{
public string Error;
public List<String> FilesList;
}
在我的ViewModel,我用下面的LINQ查詢填充的集合。 errorCollection是DataContext的一部分。
var results = (from p in Transactions
group p.FileName by p.Error.ToString()
into g
select new ErrorDetail() {Error = g.Key, FilesList = g.ToList()}).ToList();
errorCollection = new ObservableCollection<ErrorDetail>(results);
在TreeView上,我想顯示每個錯誤及其關聯文件作爲其子節點。
下面是我的XAML
<Grid Name="ErrorView" Margin="10">
<TreeView ItemsSource="{Binding Path=ErrorModel.ErrorDetails}">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Path=FilesList}">
<TextBlock Foreground="Red" Text="{Binding Path=Error}" />
<HierarchicalDataTemplate.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=FilesList}" />
</DataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
</Grid>
我懷疑在第二TextBlock的綁定。無論如何,我嘗試了多種組合,但沒有顯示出來。
請指導。
編輯:添加更多的最終結果,集合填充正確,但沒有顯示在TreeView上。
什麼是錯誤?子節點沒有顯示? –
@SzabolcsDézsi不只是孩子的節點。沒有任何節點出現。樹視圖是空的。 –
請按照以下建議來調試您的wpf綁定:http://stackoverflow.com/questions/337023/how-to-detect-broken-wpf-data-binding – Taterhead