一区二区三区日韩精品-日韩经典一区二区三区-五月激情综合丁香婷婷-欧美精品中文字幕专区

分享

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控件研究,本篇來(lái)研究下wpf的這個(gè)控件。

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

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

修改樣式后效果:

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

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

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

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

不過(guò)以上兩個(gè)都不妨礙修改效仿著改模板^^

首先說(shuō)下前臺(tái):

 <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í)縮進(jìn)放在了TreeView.ItemTemplate設(shè)置。因?yàn)椴荒苡胹ilverlight的那套東東。首先WPF里木有System.Windows.Controls.Toolkit.dll。后來(lái)我寫了自己的方法調(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,所以我只能作罷。在類里加了一個(gè)Level屬性,后臺(tái)賦值即可。

 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)來(lái)啦!~現(xiàn)在開始分析TreeViewItem的模板啦!~

第一步,將三列兩行的Grid變成一個(gè)StackPanel

第二步,增加一個(gè)treeviewitem的狀態(tài),默認(rèn)情況下只有選中的狀態(tài),這個(gè)比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>                         

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

效仿silverlight的做法,可以在模板里面添加幾個(gè)表示狀態(tài)的Rectangle,然后在trigger里面控制這幾個(gè)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>

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

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多

    日韩精品福利在线观看| 黄片免费播放一区二区| 亚洲熟女国产熟女二区三区| 污污黄黄的成年亚洲毛片| 免费午夜福利不卡片在线 视频| 亚洲精品一区二区三区免| 日韩精品一区二区不卡| 国产高清三级视频在线观看| 国产精品丝袜一二三区| 四十女人口红哪个色好看| 亚洲五月婷婷中文字幕| 粉嫩内射av一区二区| 91香蕉国产观看免费人人| 91欧美日韩精品在线| 日本深夜福利在线播放| 久久这里只精品免费福利| 国产精品一区二区成人在线| 欧美区一区二在线播放| 高清一区二区三区不卡免费| 91爽人人爽人人插人人爽| 在线观看欧美视频一区| 国产成人人人97超碰熟女| 日本女人亚洲国产性高潮视频| 国产一级性生活录像片| 人妻内射精品一区二区| 亚洲国产欧美精品久久| 国产午夜精品亚洲精品国产| 激情五月综五月综合网| 欧美一区二区日韩一区二区| 国产三级欧美三级日韩三级| 日韩欧美三级视频在线| 日韩一区二区三区有码| 东京不热免费观看日本| 高跟丝袜av在线一区二区三区| 日韩国产亚洲欧美激情| 日本欧美一区二区三区高清| 不卡视频在线一区二区三区| a久久天堂国产毛片精品| 99久久成人精品国产免费| 国产日产欧美精品大秀| 好吊色欧美一区二区三区顽频 |