博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
wpf -----Expander自定义样式
阅读量:5072 次
发布时间:2019-06-12

本文共 5188 字,大约阅读时间需要 17 分钟。

Expander是一个可以展开和折叠的控件,由ContentPresenter和ToggleButton组成

<!--Expander样式-->

<Style x:Key="ExpanderStyle" TargetType="{x:Type Expander}">
<Setter Property="Foreground" Value="Black"/>
<Setter Property="FontFamily" Value="宋体" />
<Setter Property="FontSize" Value="12" />
<Setter Property="Background" Value="#ffffff"/>
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
<Setter Property="VerticalContentAlignment" Value="Stretch"/>
<Setter Property="BorderBrush" Value="Transparent"/>
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="Width" Value="276"/>
<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 ToggleButtonStyle}" />
<Border x:Name="ExpandSite" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
Visibility="Collapsed"
Canvas.Top="0" Canvas.Left="0"
Focusable="false"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Background="#ffffff" ScrollViewer.VerticalScrollBarVisibility="Hidden">
<ContentPresenter Margin="10 0"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
</Border>

</Canvas>

<ControlTemplate.Triggers>
<Trigger Property="IsExpanded" Value="true">
<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>

 

 

<!--ToggleButton样式-->

<Style x:Key="ToggleButtonStyle" TargetType="{x:Type ToggleButton}">
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
<Setter Property="Width" Value="275"/>
<Setter Property="Height" Value="25" />
<Setter Property="Background" Value="#ffffff" />
<Setter Property="Foreground" Value="#FF217c"/>
<Setter Property="FontSize" Value="12" />
<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="10 5"/>
<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">
<Rectangle Width="275" Height="35" Fill="Red" Canvas.Right="0"/>
<Path Data="M 0 0 L 6 -6 12 0 " Stroke="#7D7D7D" StrokeThickness="2" Canvas.Right="5" Canvas.Top="18"/>
<!--<Rectangle Width="{TemplateBinding Width}" Height="1" Canvas.Bottom="0" Fill="#E3E3E3" />-->
</Canvas>
<Canvas x:Name="canvChecked" Visibility="Hidden"
Width="{TemplateBinding Width}" Height="{TemplateBinding Height}"
Background="{TemplateBinding Background}" SnapsToDevicePixels="True" Canvas.Right="0">
<Path Data="M 0 0 L 6 6 12 0 " Stroke="#7D7D7D" StrokeThickness="2" Canvas.Right="5" Canvas.Top="10"/>
<Rectangle Width="{TemplateBinding Width}" Height="1" Canvas.Bottom="0" Fill="#E3E3E3" />
</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="Red"/>
</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>

 

主程序代码:

<Expander ExpandDirection="Down " Width="276"

HorizontalAlignment="Left" IsExpanded="true"
Style="{DynamicResource ExpanderStyle}" Height="24" VerticalAlignment="Bottom" >
<Expander.Header>
<TextBlock Text="2.分析图" Name="abc" PreviewMouseLeftButtonDown="CloseClick"/>
</Expander.Header>
<!--<Expander.Content>
<TextBlock Text="文本内容" TextWrapping="Wrap" Name="asdf" />
</Expander.Content>-->
</Expander>

 

 参考的链接为:

 

转载于:https://www.cnblogs.com/lovebear123/p/11598977.html

你可能感兴趣的文章
poj1981 Circle and Points 单位圆覆盖问题
查看>>
POP的Stroke动画
查看>>
线程同步机制初识 【转载】
查看>>
SQL语句在查询分析器中可以执行,代码中不能执行
查看>>
yii 1.x 添加 rules 验证url数组
查看>>
html+css 布局篇
查看>>
SQL优化
查看>>
用C语言操纵Mysql
查看>>
轻松学MVC4.0–6 MVC的执行流程
查看>>
redis集群如何清理前缀相同的key
查看>>
Python 集合(Set)、字典(Dictionary)
查看>>
获取元素
查看>>
proxy写监听方法,实现响应式
查看>>
cocos2dx CCEditBox
查看>>
第一阶段冲刺06
查看>>
十个免费的 Web 压力测试工具
查看>>
EOS生产区块:解析插件producer_plugin
查看>>
lintcode-easy-Remove Element
查看>>
mysql重置密码
查看>>
jQuery轮 播的封装
查看>>