带有+和-的WPF Expander按钮样式是一种用于展开和折叠内容的控件,它可以在Windows Presentation Foundation(WPF)应用程序中使用。以下是如何创建一个带有+和-按钮样式的WPF Expander控件:
<Expander Header="Expander" Name="MyExpander" IsExpanded="True">
<TextBlock>这是一个可展开和折叠的内容区域。</TextBlock>
</Expander>
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style x:Key="CustomExpanderStyle" TargetType="{x:Type Expander}">
<Setter Property="HeaderTemplate">
<Setter.Value>
<DataTemplate>
<Grid Width="20" Height="20">
<TextBlock Text="{Binding}" VerticalAlignment="Center" HorizontalAlignment="Center"/>
<Path Name="ExpanderButton" Data="M0,0 L10,10 L20,0" Stroke="Black" StrokeThickness="2"
HorizontalAlignment="Right" VerticalAlignment="Center" Margin="0,0,5,0"/>
</Grid>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding ElementName=MyExpander, Path=IsExpanded}" Value="True">
<Setter TargetName="ExpanderButton" Property="RenderTransform">
<Setter.Value>
<RotateTransform Angle="180"/>
</Setter.Value>
</Setter>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
<ResourceDictionary Source="CustomExpanderStyle.xaml"/>
</Application.Resources>
<Expander Header="Expander" Name="MyExpander" IsExpanded="True" Style="{StaticResource CustomExpanderStyle}">
<TextBlock>这是一个可展开和折叠的内容区域。</TextBlock>
</Expander>
现在,当您运行应用程序时,Expander控件的标题旁边将显示一个带有+和-的按钮,当您单击该按钮时,内容区域将展开或折叠。
领取专属 10元无门槛券
手把手带您无忧上云