重寫TreeView模板來實現資料分層展示(二)

來源:互聯網
上載者:User

標籤:device   控制   stack   個人   tar   template   etc   path   方式   

  前面一片文章實現TreeView的基本的模板重寫,那麼照著這個思路,我們再來寫一個稍稍複雜的TreeView ,其它的內容都和前面系列內容相似,還是和之前文章介紹的一樣,首先看看做出的DEMO的最終樣式,先來一睹為快。

其實並不複雜,關鍵的是要準確去理解TreeView這個控制項,這個控制項的核心是TreeViewItem,那麼我們就重點來看看這個樣式該怎麼寫。

 <Style TargetType="{x:Type TreeViewItem}">            <Setter Property="Background" Value="Transparent"/>                       <!--此處設定TreeViewItem的綁定樣式-->            <Setter Property="IsExpanded" Value="True"></Setter>            <Setter Property="HorizontalContentAlignment" Value="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>            <Setter Property="VerticalContentAlignment" Value="{Binding VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>            <Setter Property="Padding" Value="1,0,0,0"/>            <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>            <Setter Property="FocusVisualStyle" Value="{StaticResource TreeViewItemFocusVisual}"/>               <Setter Property="ItemsPanel">                <Setter.Value>                    <ItemsPanelTemplate>                        <StackPanel Orientation="Horizontal" IsItemsHost="True" Margin="10"></StackPanel>                    </ItemsPanelTemplate>                                  </Setter.Value>            </Setter>                            <Setter Property="Template">                <Setter.Value>                    <ControlTemplate TargetType="{x:Type TreeViewItem}">                        <Grid>                            <Grid.ColumnDefinitions>                                <ColumnDefinition Width="Auto" MinWidth="19"/>                                <ColumnDefinition Width="*"/>                                <ColumnDefinition Width="Auto"/>                            </Grid.ColumnDefinitions>                            <Grid.RowDefinitions>                                <RowDefinition Height="Auto" MinHeight="22"/>                                <RowDefinition  Height="Auto"/>                                <RowDefinition  Height="Auto"/>                            </Grid.RowDefinitions>                                                      <Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}"                                     BorderThickness="{TemplateBinding BorderThickness}"                                     Background="#00a3d9"                                     Grid.Row="0"                                    Grid.Column="1"                                     Padding="{TemplateBinding Padding}"                                     SnapsToDevicePixels="true"                                    MaxWidth="300"                                    HorizontalAlignment="Center"                                    VerticalAlignment="Center">                                <ContentPresenter x:Name="PART_Header"                                                   ContentSource="Header"                                                  HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"                                                   SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>                            </Border>                            <Rectangle x:Name="VerLn" Grid.Row="1" Grid.Column="1" Width="1" Stroke="Green" HorizontalAlignment="Center" VerticalAlignment="Stretch" Height="50" SnapsToDevicePixels="true" Fill="White"/>                            <Border x:Name="itemsBorder"                                     BorderBrush="#00a3d9"                                    BorderThickness="1"                                                                        Padding="2"                                    Margin="10,0,10,0"                                    Grid.ColumnSpan="3"                                     Grid.Column="0"                                     Grid.Row="2"                                     HorizontalAlignment="Center"                                    VerticalAlignment="Center">                                   <ItemsPresenter x:Name="ItemsHost" />                            </Border>                        </Grid>                        <ControlTemplate.Triggers>                            <Trigger Property="IsExpanded" Value="false">                                <Setter Property="Visibility" TargetName="ItemsHost" Value="Collapsed"/>                            </Trigger>                            <Trigger Property="HasItems" Value="false">                                <Setter Property="Height" TargetName="VerLn" Value="0"/>                                <Setter Property="Height" TargetName="itemsBorder" Value="0"/>                            </Trigger>                                                        <Trigger Property="IsSelected" Value="true">                                <Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>                                <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/>                            </Trigger>                                                        <MultiTrigger>                                <MultiTrigger.Conditions>                                    <Condition Property="IsSelected" Value="true"/>                                    <Condition Property="IsSelectionActive" Value="false"/>                                </MultiTrigger.Conditions>                                <Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>                                <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>                            </MultiTrigger>                            <Trigger Property="IsEnabled" Value="false">                                <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>                            </Trigger>                        </ControlTemplate.Triggers>                    </ControlTemplate>                </Setter.Value>            </Setter>            <Style.Triggers>                <Trigger Property="VirtualizingStackPanel.IsVirtualizing" Value="true">                    <Setter Property="ItemsPanel">                        <Setter.Value>                            <ItemsPanelTemplate>                                <VirtualizingStackPanel/>                            </ItemsPanelTemplate>                        </Setter.Value>                    </Setter>                </Trigger>            </Style.Triggers>        </Style>

  在這裡我們把整個Grid分成了三行和三列,第0行第一列來來呈現我們每一個TreeViewItem的Header屬性,我們讓其置中顯示,在第1行第一列我們來顯示一條直線,這裡我們也是用的Rectangle,當然使用Border和使用Path的效果其實是一樣的,這個每個人習慣不同,最後面一行我們來顯示ItemsPresenter,這個和上面一篇形式上是差不多的,我們就不詳細介紹,這裡需要重點注意的是下面的內容。

<Setter Property="ItemsPanel">                <Setter.Value>                    <ItemsPanelTemplate>                        <StackPanel Orientation="Horizontal" IsItemsHost="True" Margin="10"></StackPanel>                    </ItemsPanelTemplate>                                  </Setter.Value>            </Setter>     

  即ItemsPresenter是放在一個容器中的,這個容器決定了裡面的Item到底需要通過怎樣的方式來呈現,這裡我們是使用的是橫向排列的StackPanel控制項來作為父容器的,另外需要注意到這個常用的Trigger

  <Trigger Property="HasItems" Value="false">                                <Setter Property="Height" TargetName="VerLn" Value="0"/>                                <Setter Property="Height" TargetName="itemsBorder" Value="0"/>                            </Trigger>                   

  這個在TreeView控制項中是非常常用的,這裡需要特別注意。

     接下裡我們再看看當前的TreeView的具體資料繫結和其它的資料邏輯。

<TreeView Name="trvMenu" HorizontalContentAlignment="Center" VerticalContentAlignment="Center">                        <TreeView.ItemTemplate>                                <HierarchicalDataTemplate DataType="{x:Type self:MyTreeViewItem}"                                           ItemsSource="{Binding Children}">                    <StackPanel Orientation="Horizontal" Margin="10">                        <Ellipse x:Name="ellipse" Width="16" Height="16" Margin="2,0,2,0" Fill="Transparent">                            <Ellipse.Triggers>                                <EventTrigger  RoutedEvent="Loaded">                                    <BeginStoryboard>                                        <Storyboard  AutoReverse="True" RepeatBehavior="Forever">                                            <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)" Storyboard.TargetName="ellipse">                                                <EasingColorKeyFrame KeyTime="0:0:0.0" Value="Transparent"/>                                                <EasingColorKeyFrame KeyTime="0:0:0.5" Value="DarkGreen"/>                                            </ColorAnimationUsingKeyFrames>                                        </Storyboard>                                    </BeginStoryboard>                                </EventTrigger>                            </Ellipse.Triggers>                        </Ellipse>                        <Image x:Name="image"                                 Source="/TestTreeView;component/Images/connect.png"                               Margin="2,0,2,0"                               Width="24"                                Height="24"                               Stretch="Fill">                                                    </Image>                        <TextBlock Text="{Binding TreeViewItemName}" VerticalAlignment="Center" FontSize="16" Foreground="White" Margin="2,0,2,0"/>                    </StackPanel>                           <HierarchicalDataTemplate.Triggers>                        <DataTrigger Binding="{Binding NodeType}" Value="採集伺服器">                            <Setter Property="Source" TargetName="image" Value="/TestTreeView;component/Images/connect.png"></Setter>                        </DataTrigger>                        <DataTrigger Binding="{Binding NodeType}" Value="控制伺服器">                            <Setter Property="Source" TargetName="image" Value="/TestTreeView;component/Images/medium_alarm_hit.png"></Setter>                        </DataTrigger>                    </HierarchicalDataTemplate.Triggers>                </HierarchicalDataTemplate>            </TreeView.ItemTemplate>

  這裡需要注意的是當前的TreeViewItem直接是作用在TreeView上的,如果TreeViewItem添加了x:Key屬性,那麼該如何引用該TreeViewItem樣式呢?正確的方式是設定TreeView的ItemContainerStyle屬性,這裡需要注意,這篇文章就介紹到這裡,如果能夠與前面的一篇內容結合起來看,那麼相信對TreeView控制項會有更多的理解吧!

 

重寫TreeView模板來實現資料分層展示(二)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.