WPF Expander style

Posted 积跬步---行千里

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WPF Expander style相关的知识,希望对你有一定的参考价值。

    <!--ExpanderStyle-->
            <Style x:Key="ExpanderStyleOne" TargetType="{x:Type Expander}">
                <Setter Property="Foreground" Value="#FF217cb5"/>
                <Setter Property="FontFamily" Value="宋体" />
                <Setter Property="FontSize" Value="14" />
                <Setter Property="Background" Value="Transparent"/>
                <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
                <Setter Property="VerticalContentAlignment" Value="Stretch"/>
                <Setter Property="BorderBrush" Value="Transparent"/>
                <Setter Property="BorderThickness" Value="0"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type Expander}">
                            <Canvas Background="{TemplateBinding Background}" SnapsToDevicePixels="true">
                                <ToggleButton x:Name="HeaderSite" 
                          Canvas.Top="0"
                          ContentTemplate="{TemplateBinding HeaderTemplate}" 
                          ContentTemplateSelector="{TemplateBinding HeaderTemplateSelector}"
                          Content="{TemplateBinding Header}"  
                          IsChecked="{Binding IsExpanded, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"                                  
                          Style="{DynamicResource ToggleButtonStyleOne}"/>
                                <Border x:Name="ExpandSite"  HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
                                        VerticalAlignment="{TemplateBinding VerticalContentAlignment}"                            
                                        Visibility="Collapsed" 
                                        Canvas.Top="56" Canvas.Left="0"
                                        Focusable="false"
                                        BorderBrush="{TemplateBinding BorderBrush}"
                                        BorderThickness="{TemplateBinding BorderThickness}" 
                                        Width="{Binding ElementName=HeaderSite,Path=Width}"
                                        Background="#FF6a8d9c">
                                    <ContentPresenter Margin="0 0"
                                    HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
                                    VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
                                </Border>
                            </Canvas>
                            <ControlTemplate.Triggers>
                                <Trigger Property="IsExpanded" Value="False">
                                    <Setter Property="Visibility" TargetName="ExpandSite" Value="Visible"/>
                                </Trigger>
                                <Trigger Property="IsEnabled" Value="false">
                                    <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
            <Style x:Key="ToggleButtonStyleOne" TargetType="{x:Type ToggleButton}">
                <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
                <Setter Property="Width" Value="285"/>
                <Setter Property="Height" Value="56" />
                <Setter Property="Background" Value="{DynamicResource ReviewGridbackground}" />
                <Setter Property="Foreground" Value="#FF217cb5"/>
                <Setter Property="FontSize" Value="14" />
                <Setter Property="FontFamily" Value="宋体" />
                <Setter Property="FontWeight" Value="Bold" />
                <Setter Property="BorderThickness" Value="0"/>
                <Setter Property="HorizontalContentAlignment" Value="Left"/>
                <Setter Property="VerticalContentAlignment" Value="Center"/>
                <Setter Property="Padding" Value="0 0"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type ToggleButton}">
                            <Canvas Width="{TemplateBinding Width}" Height="{TemplateBinding Height}" 
                                Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
                                <Canvas x:Name="canvNormal" Visibility="Visible" 
                                 Width="{TemplateBinding Width}" Height="{TemplateBinding Height}" 
                                 Background="{TemplateBinding Background}" SnapsToDevicePixels="True" Canvas.Right="0">
                                    <Button Width="235" Height="56" Opacity="0"/>
                                    <Rectangle Width="15" Height="56" Fill="#4a5f6a" Canvas.Right="0"/>
                                    <Image Source="../Image/1.PNG"  Margin="235,0,0,0" Height="55"  Width="15"/>
                                    <!--<Path Data="M13.494,10.697l-3.497-3.6l-3.497,3.6L6.494,8.328l3.503-3.631l3.48,3.592L13.494,10.697z" Fill="#297eb4" Canvas.Right="4" Canvas.Top="15"/>
                            <Path Data="M13.494,10.697l-3.497-3.6l-3.497,3.6L6.494,8.328l3.503-3.631l3.48,3.592L13.494,10.697z" Fill="#297eb4" Canvas.Right="4" Canvas.Top="25"/>-->
                                    <!--<Line X1="0" Y1="0" X2="0" Y2="15" Stroke="#297eb4" StrokeThickness="3" 
                                    Margin="6 10" StrokeDashArray="1" Canvas.Right="0"/>-->
                                    <!--<Rectangle Width="{TemplateBinding Width}" Height="1" Canvas.Bottom="0" Fill="#FFbacfda" />-->
                                </Canvas>
                                <Canvas x:Name="canvChecked" Visibility="Hidden" 
                                 Width="{TemplateBinding Width}" Height="{TemplateBinding Height}" 
                                 Background="{TemplateBinding Background}" SnapsToDevicePixels="True" Canvas.Right="0">
                                    <Button Width="235" Height="56" Opacity="0"/>
                                    <Rectangle Width="15" Height="56" Fill="#4a5f6a" Canvas.Right="0"/>
                                    <!--<Path Data="M13.494,10.697l-3.497-3.6l-3.497,3.6L6.494,8.328l3.503-3.631l3.48,3.592" Fill="#297eb4" Canvas.Right="4" Canvas.Top="8"/>
                                <Path Data="M13.494,10.697l-3.497-3.6l-3.497,3.6L6.494,8.328l3.503-3.631l3.48,3.592" Fill="#297eb4" Canvas.Right="4" Canvas.Top="15"/>-->
                                    <Image Source="../Image/2.PNG"  Margin="235,0,0,0"  Height="55"  Width="15"/>
                                    <!--<Rectangle Width="{TemplateBinding Width}" Height="1" Canvas.Bottom="0" Fill="#FFbacfda" />-->
                                </Canvas>
                                <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
                                                 Margin="{TemplateBinding Padding}" 
                                                 RecognizesAccessKey="True" 
                                                 SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" 
                                                 VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                            </Canvas>

                            <ControlTemplate.Triggers>
                                <!--<Trigger Property="IsPressed" Value="true">
                            <Setter Property="Background" Value="#FFd2e7f4" />
                        </Trigger>-->
                                <Trigger Property="IsChecked" Value="true">
                                    <Setter Property="Visibility" TargetName="canvChecked" Value="Visible" />
                                    <Setter Property="Visibility" TargetName="canvNormal" Value="Hidden" />
                                </Trigger>
                                <!--<Trigger Property="IsEnabled" Value="false">
                            <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                        </Trigger>-->
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
            <Style x:Key="ExpanderStyleTwo" TargetType="{x:Type Expander}">
                <Setter Property="Foreground" Value="#FF217cb5"/>
                <Setter Property="FontFamily" Value="宋体" />
                <Setter Property="FontSize" Value="14" />
                <Setter Property="Background" Value="Transparent"/>
                <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
                <Setter Property="VerticalContentAlignment" Value="Stretch"/>
                <Setter Property="BorderBrush" Value="Transparent"/>
                <Setter Property="BorderThickness" Value="0"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type Expander}">
                            <Canvas Background="{TemplateBinding Background}" SnapsToDevicePixels="true">
                                <ToggleButton x:Name="HeaderSite" 
                          Canvas.Top="0"
                          ContentTemplate="{TemplateBinding HeaderTemplate}" 
                          ContentTemplateSelector="{TemplateBinding HeaderTemplateSelector}"
                          Content="{TemplateBinding Header}"  
                          IsChecked="{Binding IsExpanded, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"                                  
                          Style="{DynamicResource ToggleButtonStyleTwo}"/>
                                <Border x:Name="ExpandSite"  HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
                                        VerticalAlignment="{TemplateBinding VerticalContentAlignment}"                            
                                        Visibility="Collapsed" 
                                        Canvas.Top="35" Canvas.Left="0"
                                        Focusable="false"
                                        BorderBrush="{TemplateBinding BorderBrush}"
                                        BorderThickness="{TemplateBinding BorderThickness}" 
                                        Width="{Binding ElementName=HeaderSite,Path=Width}"
                                        Background="#4a5f6a">
                                    <ContentPresenter Margin="0 0" 
                                    HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
                                    VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
                                </Border>
                            </Canvas>
                            <ControlTemplate.Triggers>
                                <Trigger Property="IsExpanded" Value="False">
                                    <Setter Property="Visibility" TargetName="ExpandSite" Value="Visible"/>
                                </Trigger>
                                <Trigger Property="IsEnabled" Value="false">
                                    <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
            <Style x:Key="ToggleButtonStyleTwo" TargetType="{x:Type ToggleButton}">
                <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
                <Setter Property="Width" Value="250"/>
                <Setter Property="Height" Value="56" />
                <Setter Property="Background" Value="{DynamicResource ReviewGridbackground}" />
                <Setter Property="Foreground" Value="#FF217cb5"/>
                <Setter Property="FontSize" Value="14" />
                <Setter Property="FontFamily" Value="宋体" />
                <Setter Property="FontWeight" Value="Bold" />
                <Setter Property="BorderThickness" Value="0"/>
                <Setter Property="HorizontalContentAlignment" Value="Left"/>
                <Setter Property="VerticalContentAlignment" Value="Center"/>
                <Setter Property="Padding" Value="0 0"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type ToggleButton}">
                            <Canvas Width="{TemplateBinding Width}" Height="{TemplateBinding Height}" 
                                Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
                                <Canvas x:Name="canvNormal" Visibility="Visible" 
                                 Width="{TemplateBinding Width}" Height="{TemplateBinding Height}" 
                                 Background="{TemplateBinding Background}" SnapsToDevicePixels="True" Canvas.Right="0">
                                    <Button Width="250" Height="{TemplateBinding Height}" Opacity="0"/>
                                    <!--<Rectangle Width="15" Height="35" Fill="#FFc7ebff" Canvas.Right="0"/>-->
                                    <!--<Path Data="M13.494,10.697l-3.497-3.6l-3.497,3.6L6.494,8.328l3.503-3.631l3.48,3.592L13.494,10.697z" Fill="#297eb4" Canvas.Right="4" Canvas.Top="8"/>
                            <Path Data="M13.494,10.697l-3.497-3.6l-3.497,3.6L6.494,8.328l3.503-3.631l3.48,3.592L13.494,10.697z" Fill="#297eb4" Canvas.Right="4" Canvas.Top="15"/>-->
                                    <!--<Line X1="0" Y1="0" X2="0" Y2="15" Stroke="#297eb4" StrokeThickness="3" 
                                    Margin="6 10" StrokeDashArray="1" Canvas.Right="0"/>-->
                                </Canvas>
                                <Canvas x:Name="canvChecked" Visibility="Hidden" 
                                 Width="{TemplateBinding Width}" Height="{TemplateBinding Height}" 
                                 Background="{TemplateBinding Background}" SnapsToDevicePixels="True" Canvas.Right="0">
                                    <Button Width="250" Height="{TemplateBinding Height}" Opacity="0"/>
                                    <!--<Rectangle Width="15" Height="35" Fill="#FFc7ebff" Canvas.Right="0"/>-->
                                    <!--<Path Data="M13.494,10.697l-3.497-3.6l-3.497,3.6L6.494,8.328l3.503-3.631l3.48,3.592" Fill="#297eb4" Canvas.Right="4" Canvas.Top="8"/>
                                <Path Data="M13.494,10.697l-3.497-3.6l-3.497,3.6L6.494,8.328l3.503-3.631l3.48,3.592" Fill="#297eb4" Canvas.Right="4" Canvas.Top="15"/>-->
                                    <!--<Image Source="image/1.PNG"  Margin="228,-1,0,0"/>-->
                                    <Rectangle Width="{TemplateBinding Width}" Height="1" Canvas.Bottom="0" Fill="#FFbacfda" />
                                </Canvas>
                                <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
                                                 Margin="{TemplateBinding Padding}" 
                                                 RecognizesAccessKey="True" 
                                                 SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" 
                                                 VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                            </Canvas>

                            <!--<ControlTemplate.Triggers>
                        <Trigger Property="IsPressed" Value="true">
                            <Setter Property="Background" Value="#FFd2e7f4" />
                  

以上是关于WPF Expander style的主要内容,如果未能解决你的问题,请参考以下文章

WPF中 怎么禁用 Expander.Header中的ToggleButton 要源代码呀~~

WPF MVVM阻止Expander控件崩溃

WPF效果第一百八十八篇之再玩Expander

WPF 自定义Expander

WPF TreeView,Expander样式使用

wpf 列表菜单 收起与展开,通过Grid DoubleAnimation或者Expander实现