[英]WPF Complex Hierarchical data template
我正在尋找一個復雜的結構,我似乎找不到顯示它的方法......
我的情況:
類:里程碑里面有2個列表,其他子里程碑列表和活動列表。
如此結構化可能像:
M1
有人對如何創建這個有任何想法嗎? 或者可以把我推向一個方向?
回答我的問題
<TreeView ItemsSource="{Binding Path=Project.TrackList}">
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type deto:Track}" ItemsSource="{Binding Path=FaseList}">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=TrackType}" />
</StackPanel>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type deto:Fase}" ItemsSource="{Binding Path=MilestoneList}">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=FaseType}" />
</StackPanel>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type deto:Milestone}" ItemsSource="{Binding Converter={StaticResource MConverter}}">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=Description}" />
</StackPanel>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type deto:Activity}" ItemsSource="{Binding Path=ActivityList}">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=Description}" />
</StackPanel>
</HierarchicalDataTemplate>
</TreeView.Resources>
</TreeView>
和轉換器:
public class MilestoneConverter : IValueConverter
{
#region IValueConverter Members
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
var m = value as Milestone;
CompositeCollection collection = new CompositeCollection();
collection.Add(new CollectionContainer()
{
Collection = m.MilestoneList
});
collection.Add(new CollectionContainer()
{
Collection = m.ActivityList
});
return collection;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
#endregion
}
例如,您應該能夠使用CompositeCollection
執行此操作。 在Xaml中執行它在引用源方面可能有點復雜,但在這種情況下使用轉換器應該是可接受的:
public class MilestoneItemsSourceCreator : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
var input = value as Milestone;
CompositeCollection collection = new CompositeCollection();
collection.Add(new CollectionContainer(){ Collection = input.SubMilestones });
collection.Add(new CollectionContainer(){ Collection = input.Activities });
return collection;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotSupportedException();
}
}
<vc:MilestoneItemsSourceCreator x:Key="MilestoneItemsSourceCreator"/>
<HierarchicalDataTemplate DataType="{x:Type local:Milestone}"
ItemsSource="{Binding Converter={StaticResource MilestoneItemsSourceCreator}}">
<!-- DataTemplate -->
</HierarchicalDataTemplate>
這可能不完全適合您的類結構,但您沒有明確發布,可能需要進行一些調整。
我過去做過類似的事。
您應該在另一個上方顯示兩個列表控件(例如ListView / ListBox),並將第二個數據綁定到第一個選定項目。
在您的情況下,似乎您需要3個列表控件,隨意適應。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.