电竞比分网-中国电竞赛事及体育赛事平台

分享

TreeView控件研究(WPF篇)(創(chuàng)建+數(shù)據(jù)綁定+整行選中)

 牛人的尾巴 2016-04-03

TreeView控件研究(WPF篇)(創(chuàng)建+數(shù)據(jù)綁定+整行選中)

(2013-03-11 10:46:58)
標(biāo)簽:

treeview

wpf

分類: SL/WPF控件研究

接著上篇silverlight的treeview控件研究,本篇來研究下wpf的這個控件。

默認(rèn)情況下的效果:

TreeView控件研究(WPF篇)(創(chuàng)建+數(shù)據(jù)綁定+整行選中) 默認(rèn)情況下treeviewItem只有選中效果,且不能整行選中

修改樣式后效果:

TreeView控件研究(WPF篇)(創(chuàng)建+數(shù)據(jù)綁定+整行選中) 增加了treeviewItem的鼠標(biāo)經(jīng)過狀態(tài),提供整行選中效果

首先分析下treeview控件在silverlight和wpf下模板的不同:

1. wpf用了trigger控制模板內(nèi)控件的屬性變化,silverlight用了svm

2. wpf的treeview模板相比silverlgit下的少了一層Button。

不過以上兩個都不妨礙修改效仿著改模板^^

首先說下前臺:

 <TreeView x:Name="MyTV" Width="250" Padding="0" Margin="0" BorderThickness="1" >
                <TreeView.ItemTemplate>
                    <HierarchicalDataTemplate ItemsSource="{Binding Children}">
                        <TextBlock x:Name="txt" VerticalAlignment="Center" Text="{Binding DisplayName}" Margin="{Binding Level, Converter={StaticResource IndentConverter}}"/>
                    </HierarchicalDataTemplate               
                </TreeView.ItemTemplate>
            </TreeView>

相比silverlight而言不用引用額外的dll。這里我把treeview的層級縮進(jìn)放在了TreeView.ItemTemplate設(shè)置。因為不能用silverlight的那套東東。首先WPF里木有System.Windows.Controls.Toolkit.dll。后來我寫了自己的方法調(diào)用

 public static class TreeViewItemExtensions
    {
        public static int GetDepth(this TreeViewItem item)
        {
            FrameworkElement elem = item;
            while (elem.Parent != null)
            {
                var tvi = elem.Parent as TreeViewItem;
                if (tvi != null)
                {
                    return tvi.GetDepth() + 1;
                }
                elem = elem.Parent as FrameworkElement;
            }
            return 0;
        }
    }
得到的結(jié)果elem.Parent返回永遠(yuǎn)是null,所以我只能作罷。在類里加了一個Level屬性,后臺賦值即可。

 public class TvItem
    {
        public int Level { get; set; }

        public string DisplayName { get; set; }

        public List Children { get; set; }

        public TvItem()
        {
            Children = new List();
        }
    }

好了,重點(diǎn)來啦!~現(xiàn)在開始分析TreeViewItem的模板啦!~

第一步,將三列兩行的Grid變成一個StackPanel

第二步,增加一個treeviewitem的狀態(tài),默認(rèn)情況下只有選中的狀態(tài),這個比silverlight的要偷懶啊 = =

  <MultiTrigger>
    <MultiTrigger.Conditions>
    <Condition Property="IsMouseOver" Value="true"/>
     <Condition Property="IsExpanded" Value="false"/>
     <Condition Property="IsSelected" Value="false"/>
   </MultiTrigger.Conditions><Setter Property="Background"  TargetName="Bd"Value="yellow"/>                                                          </MultiTrigger>                         

好啦,目前為止,基本功能都實現(xiàn)啦。如果對樣式要求更高,實現(xiàn)起來也很方便的。

效仿silverlight的做法,可以在模板里面添加幾個表示狀態(tài)的Rectangle,然后在trigger里面控制這幾個Rectangle的Opacity即可。

最后獻(xiàn)上TreeViewItem的樣式代碼:

   <Style TargetType="{x:Type TreeViewItem}">
            <Setter Property="Background" Value="Transparent"/>
            <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="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type TreeViewItem}">
                        <StackPanel>
                            <Border x:Name="Bd" CornerRadius="2" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true">                              
                                <Grid>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="auto"/>
                                        <ColumnDefinition/>
                                    </Grid.ColumnDefinitions>

                                    <Rectangle x:Name="Hover" Grid.ColumnSpan="2" Stroke="#FFd8f0ff" StrokeThickness="1" HorizontalAlignment="Stretch" RadiusX="4" RadiusY="4" IsHitTestVisible="False" Opacity="0">
                                        <Rectangle.Fill>
                                            <LinearGradientBrush StartPoint=".5,0" EndPoint=".5,1">
                                                <GradientStop Offset="0" Color="#ffe5f4ff" />
                                                <GradientStop Offset="1" Color="#FFd8f0ff" />
                                            </LinearGradientBrush>
                                        </Rectangle.Fill>
                                    </Rectangle>
                                    <Rectangle x:Name="Selection"  Grid.ColumnSpan="2" Stroke="#FFfee69e" HorizontalAlignment="Stretch" RadiusX="4" RadiusY="4" IsHitTestVisible="False" Opacity="0" >
                                        <Rectangle.Fill>
                                            <LinearGradientBrush>
                                                <GradientStop Offset="0" Color="#FFfff2ca" />
                                                <GradientStop Offset="1" Color="#FFfee69e" />
                                            </LinearGradientBrush>
                                        </Rectangle.Fill>
                                    </Rectangle>
                                   
                                    <ToggleButton x:Name="Expander" ClickMode="Press" IsChecked="{Binding IsExpanded, RelativeSource={RelativeSource TemplatedParent}}" Style="{StaticResource ExpandCollapseToggleStyle}"/>
                                    <ContentPresenter x:Name="PART_Header" Grid.Column="1" ContentSource="Header" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                                </Grid>
                            </Border>
                        
                            <ItemsPresenter x:Name="ItemsHost"/>
                        </StackPanel>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsExpanded" Value="false">
                                <Setter Property="Visibility" TargetName="ItemsHost" Value="Collapsed"/>
                            </Trigger>
                            <Trigger Property="HasItems" Value="false">
                                <Setter Property="Visibility" TargetName="Expander" Value="Hidden"/>
                            </Trigger>
                            <Trigger Property="IsSelected" Value="true">
                                <!--<Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>-->
                                <Setter Property="Opacity" TargetName="Selection" Value="1"/>
                                <!--<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/>-->
                            </Trigger>
                            <MultiTrigger>
                                <MultiTrigger.Conditions>
                                    <Condition Property="IsMouseOver" Value="true"/>
                                    <Condition Property="IsExpanded" Value="false"/>
                                    <Condition Property="IsSelected" Value="false"/>
                                </MultiTrigger.Conditions>
                                <!--<Setter Property="Background" TargetName="Bd" Value="yellow"/>-->
                                <Setter Property="Opacity" TargetName="Hover" Value="1"/>
                            </MultiTrigger>
                            <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="Opacity" TargetName="Selection" Value="1"/>
                                <!--<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>

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多