2010-11-30 63 views
2

所以,以下WPF中很容易,但您如何在Silverlight中做到這一點?Silverlight與WPF - Treeview與HierarchialDataTemplate

請注意,這裏的技巧是同時顯示組和條目。 另外你不知道條目嵌套的深度,他們可能在第一或第n層。 由於缺少H.DataTemplate(或任何DataTemplate)中的DataType =「{x:Type local:Group}」屬性,因此這在Silverlight中很難實現。構建我自己的自定義DataTempalteSelector也沒有用,因爲Hierarchial ItemsSource會丟失。 (這只是給了我一個新的想法,我會盡快調查)


例子:

Group1 
--Entry 
--Entry 
Group2 
--Group4 
----Group1 
------Entry 
------Entry 
----Entry 
----Entry 
--Entry 
--Entry 
Group3 
--Entry 
--Entry 

你的類:

public class Entry 
{ 
    public int Key { get; set; } 
    public string Name { get; set; } 
} 

public class Group 
{ 
    public int Key { get; set; } 
    public string Name { get; set; } 

    public IList<Group> SubGroups { get; set; } 
    public IList<Entry> Entries { get; set; } 
} 

XAML中:

<TreeView Name="GroupView" Grid.Row="0" Grid.Column="0" ItemsSource="{Binding}"> 
     <TreeView.Resources> 
      <HierarchicalDataTemplate DataType="{x:Type local:Group}" ItemsSource={Binding Items}"> 
       <TextBlock Text="{Binding Path=Name}" /> 
      </HierarchicalDataTemplate> 
      <DataTemplate DataType="{x:Type local:Entry}" > 
       <TextBlock Text="{Binding Path=Name}" /> 
      </DataTemplate> 
     </TreeView.Resources> 
    </TreeView> 
+0

這是否的ItemsSource = 「{綁定表項}」 真的WPF工作?這是什麼「項目」? – 2010-11-30 22:52:42

+0

好點,我沒有自己測試它,我只是提到這篇文章:http://stackoverflow.com/questions/4150334/treeview-binding那裏的片段被給出作爲答案。 – AGhosT 2010-12-02 11:07:48

+0

我suppos項目應該只是改變爲SubGroups(和TreeView中的綁定應該是obs.collection ) – AGhosT 2010-12-02 11:08:38

回答

0

Silverlight工具包中的TreeView控件支持分層數據模板。

查看這個article的示例使用情況,但它看起來與我一個WPF內置的一樣。您可以下載Silverlight工具包here

0

下面是使用HierarchicalDataTemplate用在Silverlight(Songhay.Silverlight.BiggestBox.Views.ClientView.xaml)一HeaderedItemsControl的例子:

<UserControl x:Class="Songhay.Silverlight.BiggestBox.Views.ClientView" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    mc:Ignorable="d" 
    xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation" 
    xmlns:sdk="clr-namespace:System.Windows;assembly=System.Windows.Controls" 
    xmlns:sdkctrls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls" 
    xmlns:uriMapper="clr-namespace:System.Windows.Navigation;assembly=System.Windows.Controls.Navigation" 
    xmlns:v="clr-namespace:Songhay.Silverlight.BiggestBox.Views" 
    xmlns:m="clr-namespace:Songhay.Silverlight.BiggestBox.ViewModels"> 
    <UserControl.Resources> 

     <Style x:Key="StackPanelRoot" TargetType="StackPanel"> 
      <Setter Property="Background" Value="Seashell" /> 
      <Setter Property="Height" Value="598" /> 
      <Setter Property="Width" Value="1024" /> 
     </Style> 

     <Style x:Key="GridRoot" TargetType="Grid"> 
      <Setter Property="Height" Value="570" /> 
     </Style> 

     <Style x:Key="ClientGridSplitter" TargetType="sdkctrls:GridSplitter"> 
      <Setter Property="Background" Value="#FFE0EEE0" /> 
      <Setter Property="Height" Value="8" /> 
      <Setter Property="HorizontalAlignment" Value="Stretch" /> 
     </Style> 

     <m:ClientViewModel x:Key="ClientViewModelDataSource" d:IsDataSource="True"/> 

     <Style TargetType="sdkctrls:HeaderedItemsControl"> 
      <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate TargetType="sdkctrls:HeaderedItemsControl"> 
         <StackPanel> 
          <ItemsPresenter Margin="10,0,0,0" /> 
         </StackPanel> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style> 

     <Style x:Key="ScrollViewerIndexItems" TargetType="ScrollViewer"> 
      <Setter Property="Margin" Value="10" /> 
      <Setter Property="VerticalScrollBarVisibility" Value="Auto" /> 
     </Style> 

     <Style x:Key="StackPanelIndexItems" TargetType="StackPanel"> 
      <Setter Property="Background" Value="#ff9" /> 
      <Setter Property="Orientation" Value="Horizontal" /> 
     </Style> 

    </UserControl.Resources> 

    <UserControl.DataContext> 
     <Binding Source="{StaticResource ClientViewModelDataSource}"/> 
    </UserControl.DataContext> 

    <Border BorderBrush="Black" BorderThickness="1" VerticalAlignment="Center"> 
     <StackPanel x:Name="RootPanel" Style="{StaticResource StackPanelRoot}"> 
      <Grid Style="{StaticResource GridRoot}"> 
       <Grid.RowDefinitions> 
        <RowDefinition MinHeight="128" MaxHeight="360" Height="Auto" /> 
        <RowDefinition Height="Auto" /> 
        <RowDefinition Height="1.5*" /> 
       </Grid.RowDefinitions> 
       <v:HeaderView Grid.Row="0" /> 
       <sdkctrls:GridSplitter Grid.Row="1" Style="{StaticResource ClientGridSplitter}" /> 
       <StackPanel Grid.Row="2" 
        Orientation="Horizontal" 
        Style="{StaticResource StackPanelIndexItems}"> 
        <ScrollViewer Style="{StaticResource ScrollViewerIndexItems}"> 
         <sdkctrls:HeaderedItemsControl 
          Header="{Binding IndexTitle}" 
          ItemsSource="{Binding Outlines}"> 
          <sdkctrls:HeaderedItemsControl.HeaderTemplate> 
           <DataTemplate> 
            <TextBlock FontSize="24" FontWeight="Bold" Text="{Binding}" /> 
           </DataTemplate> 
          </sdkctrls:HeaderedItemsControl.HeaderTemplate> 
          <sdkctrls:HeaderedItemsControl.ItemTemplate> 
           <sdk:HierarchicalDataTemplate> 
            <StackPanel> 
             <TextBlock FontSize="12" FontWeight="Bold" Margin="0,10,0,0" Text="{Binding Text}" /> 
             <sdkctrls:HeaderedItemsControl ItemsSource="{Binding Outlines}" Margin="10,0,10,0"> 
              <sdkctrls:HeaderedItemsControl.ItemTemplate> 
               <sdk:HierarchicalDataTemplate> 
                <HyperlinkButton 
                 ClickMode="Press" 
                 Command="{Binding IndexItemCommand, Source={StaticResource ClientViewModelDataSource}}" 
                 CommandParameter="{Binding Url}" 
                 FontSize="12"> 
                 <HyperlinkButton.Content> 
                  <TextBlock Text="{Binding Text}" /> 
                 </HyperlinkButton.Content> 
                </HyperlinkButton> 
               </sdk:HierarchicalDataTemplate> 
              </sdkctrls:HeaderedItemsControl.ItemTemplate> 
             </sdkctrls:HeaderedItemsControl> 
            </StackPanel> 
           </sdk:HierarchicalDataTemplate> 
          </sdkctrls:HeaderedItemsControl.ItemTemplate> 
         </sdkctrls:HeaderedItemsControl> 
        </ScrollViewer> 
        <navigation:Frame x:Name="IndexFrame" Width="685"> 
        </navigation:Frame> 
       </StackPanel> 
      </Grid> 
      <v:FooterView Height="30" /> 
     </StackPanel> 
    </Border> 
</UserControl>