Expander-自定义模板(很好的东西)

上一篇文章简单说了一下Expander控件,本文将编写一个自义模板的Expander控件,如下图所示:
 
 
[html]  view plain  copy
 print ?
  1. <Window x:Class="Expander_Sample2.Window1"  
  2.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  3.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  4.     Title="Window1" Height="300" Width="300">  
  5.     <Window.Resources>  
  6.           
  7.         <Style TargetType="{x:Type Expander}">  
  8.             <Setter Property="Template">  
  9.                 <Setter.Value>  
  10.                     <ControlTemplate TargetType="{x:Type Expander}">  
  11.                         <Grid>  
  12.                             <Grid.RowDefinitions>  
  13.                                 <RowDefinition Height="auto"/>  
  14.                                 <RowDefinition x:Name="gr" Height="0"/>  
  15.                             Grid.RowDefinitions>  
  16.                             <BulletDecorator Background="DarkTurquoise" Grid.Row="0" VerticalAlignment="Center" >  
  17.                                 <BulletDecorator.Bullet>  
  18.                                     <ToggleButton Margin="1,1,1,1"  Height="18" Width="18" Template="{StaticResource ToggleButtonTemp}"  
  19.                                                   IsChecked="{Binding Path=IsExpanded, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"  
  20.                                                   OverridesDefaultStyle="True"/>  
  21.                                 BulletDecorator.Bullet>  
  22.                                 <ContentPresenter HorizontalAlignment="Center" Margin="1,1,1,1" ContentSource="Header"/>  
  23.                             BulletDecorator>  
  24.                             <Border x:Name="scv" Background="LightGray" BorderThickness="1" BorderBrush="Black" Grid.Row="1" >  
  25.                                 <ContentPresenter Margin="0" ContentSource="Content"/>  
  26.                             Border>  
  27.                         Grid>  
  28.                         <ControlTemplate.Triggers>  
  29.                             <Trigger Property="IsExpanded" Value="True">  
  30.                                 <Setter TargetName="gr" Property="Height" Value="{Binding Path=DesiredSize/Height,ElementName=scv}"/>  
  31.                             Trigger>  
  32.                         ControlTemplate.Triggers>  
  33.                     ControlTemplate>  
  34.                 Setter.Value>  
  35.             Setter>  
  36.         Style>  
  37.     Window.Resources>  
  38.     <Grid>  
  39.         <Expander Margin="10,10" Height="210" Width="130" OverridesDefaultStyle="True">  
  40.             <Expander.Header>  
  41.                 <TextBlock Text="相见恨晚" FontWeight="Bold" FontSize="16"/>  
  42.             Expander.Header>  
  43.             <TextBlock TextWrapping="Wrap">  
  44.                 如果相见不会太晚,我们就不会悲伤,和你堂堂的手牵手,过得好简单,  
  45.                 若我有天不见了,或许你会比较快乐,虽然有万般舍不得,也不愿看你难割舍   
  46.                 若我有天不在了。请你原谅我的困扰,虽然你给我的不算少,只是我没福气要就算是完美。  
  47.             TextBlock>  
  48.         Expander>  
  49.     Grid>  
  50. Window>  

你可能感兴趣的:(WPF)