Silverlight与WPF-具有HierarchialDataTemplate的Treeview [英] Silverlight vs WPF - Treeview with HierarchialDataTemplate

查看:71
本文介绍了Silverlight与WPF-具有HierarchialDataTemplate的Treeview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,在WPF中执行以下操作很容易,但是您将如何在Silverlight中做到这一点?

So, the following is easy enough in WPF, but how would you do it in Silverlight?

请注意,这里的技巧是在同一级别上显示组"和条目". 另外,您不知道条目的嵌套深度,它们可能位于第一层或第n层. 这在Silverlight中很难实现,因为在H.DataTemplate(或任何DataTemplate)中缺少DataType ="{x:Type local:Group}"属性.建立我自己的自定义DataTempalteSelector也不起作用,因为Hierarchial ItemsSource丢失了. (这只是给了我一个新主意,我将在不久后进行调查)

Please note that the trick here is to display both Groups, and Entries on the same level. Additonally you dont know how deep the entries are nested, they might be on the first, or the nth level. This is hard in Silverlight because you lack the DataType="{x:Type local:Group}" property in the H.DataTemplate (or any DataTemplate). Building my own custom DataTempalteSelector also didnt work, because the Hierarchial ItemsSource gets lost. (Which just gave me a new idea which I will investigate shortly)

示例:

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


您的课程:


Your Classes:

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:


Your 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>

推荐答案

以下是在Silverlight中将HierarchicalDataTemplateHeaderedItemsControl一起使用的示例(

Here is an example of using the HierarchicalDataTemplate with a HeaderedItemsControl in Silverlight (Songhay.Silverlight.BiggestBox.Views.ClientView.xaml):

<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>

这篇关于Silverlight与WPF-具有HierarchialDataTemplate的Treeview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆