另一种Expander 自定义效果:
DemoWindowStyles.xaml:
1 <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 2 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 3 <ControlTemplate x:Key="ToggleButtonTemp" TargetType="{x:Type ToggleButton}"> 4 <Border x:Name="bd" 5 BorderThickness="1" 6 CornerRadius="1,1,1,1"> 7 <Border.Background> 8 <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1"> 9 <GradientStop Color="LightGreen" Offset="0"/> 10 <GradientStop Color="White" Offset="1"/> 11 </LinearGradientBrush> 12 </Border.Background> 13 <Border.BorderBrush> 14 <LinearGradientBrush StartPoint="0,0" EndPoint="1,1"> 15 <GradientStop Color="Black" Offset="0"/> 16 <GradientStop Color="Gray" Offset="1"/> 17 </LinearGradientBrush> 18 </Border.BorderBrush> 19 <Path Fill="Black" x:Name="p" 20 Data="M 0,0 L 4,5 L 8,0 Z" 21 HorizontalAlignment="Center" 22 VerticalAlignment="Center"/> 23 </Border> 24 <ControlTemplate.Triggers> 25 <Trigger Property="IsMouseOver" Value="True"> 26 <Setter TargetName="bd" Property="Background"> 27 <Setter.Value> 28 <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1"> 29 <GradientStop Color="DarkGreen" Offset="0"/> 30 <GradientStop Color="White" Offset="1"/> 31 </LinearGradientBrush> 32 </Setter.Value> 33 </Setter> 34 </Trigger> 35 <Trigger Property="IsChecked" Value="True"> 36 <Setter TargetName="p" Property="Data" 37 Value="M 0,5 L 8,5 L 4,0 Z"/> 38 </Trigger> 39 <Trigger Property="IsEnabled" Value="True"> 40 <Setter TargetName="bd" Property="BorderBrush" Value="Gray"/> 41 <Setter TargetName="p" Property="Fill" Value="Gray"/> 42 </Trigger> 43 </ControlTemplate.Triggers> 44 </ControlTemplate> 45 <!-- 46 Expnder的样式 47 --> 48 <Style x:Key="ExpanderStyle1" TargetType="{x:Type Expander}"> 49 <Setter Property="Template"> 50 <Setter.Value> 51 <ControlTemplate TargetType="{x:Type Expander}"> 52 <Grid> 53 <Grid.RowDefinitions> 54 <RowDefinition Height="auto"/> 55 <RowDefinition x:Name="gr" Height="auto"/> 56 </Grid.RowDefinitions> 57 <BulletDecorator Grid.Row="0" VerticalAlignment="Center" > 58 <BulletDecorator.Bullet> 59 <ToggleButton Height="18" Width="640" HorizontalAlignment="Stretch" VerticalAlignment="Center" Template="{StaticResource ToggleButtonTemp}" 60 IsChecked="{Binding Path=IsExpanded, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" 61 OverridesDefaultStyle="True" Margin="0,0"/> 62 </BulletDecorator.Bullet> 63 </BulletDecorator> 64 <Border x:Name="scv" BorderThickness="1" BorderBrush="White" Grid.Row="1" > 65 <ContentPresenter x:Name="ExpandSite" 66 HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 67 Margin="{TemplateBinding Padding}" 68 VerticalAlignment="{TemplateBinding VerticalContentAlignment}" 69 Focusable="false" Visibility="Collapsed" 70 DockPanel.Dock="Bottom"/> 71 </Border> 72 </Grid> 73 <ControlTemplate.Triggers> 74 <Trigger Property="IsExpanded" Value="True"> 75 <Setter Property="Visibility" TargetName="ExpandSite" Value="Visible"/> 76 </Trigger> 77 </ControlTemplate.Triggers> 78 </ControlTemplate> 79 </Setter.Value> 80 </Setter> 81 </Style> 82 </ResourceDictionary>
在要使用的WPF中App.xaml中:
1 <Application x:Class="******.App" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 StartupUri="MainWindow.xaml"> 5 <Application.Resources> 6 <ResourceDictionary> 7 <!--使用资源字典的合并资源功能--> 8 <ResourceDictionary.MergedDictionaries> 9 <!--在这里可以指定多个资源文件名--> 10 <ResourceDictionary Source="****/DemoWindowStyles.xaml"/> 11 </ResourceDictionary.MergedDictionaries> 12 </ResourceDictionary> 13 </Application.Resources> 14 </Application>
在MainWindow中使用:
<Expander ExpandDirection="Down" Height="53" Width="640" DockPanel.Dock="Top" Margin="0,0,0,0" IsExpanded="False" Style="{StaticResource ExpanderStyle1}"> <Menu x:Name="menu"**************** ********************************************* </Menu> </Expander>