Warrior Sun

内蒙古强网电子科技有限公司

导航

【再学WPF】模板

Posted on 2021-03-08 18:10  Warrior Sun  阅读(74)  评论(0编辑  收藏  举报
 1     <!--设置所有的按钮样式-->
 2     <Style TargetType="Button">
 3         <Setter Property="Margin" Value="0 10 0 0"/>
 4         
 5         <Setter Property="Template">
 6             <Setter.Value>
 7                 <ControlTemplate TargetType="Button">
 8                     <Border x:Name="bg" Background="#77FFFFFF" BorderBrush="Black" BorderThickness="1" CornerRadius="5">
 9                         <Grid>
10                             <ContentPresenter x:Name="_Content1" 
11                                   VerticalAlignment="Center" 
12                                   HorizontalAlignment="Center"/>
13                         </Grid>
14                     </Border>
15                     <ControlTemplate.Triggers>
16                         <Trigger Property="IsMouseOver" Value="True">
17                             <Setter Property="Opacity" Value="0.8"/>
18                             <Setter TargetName="_Content1" Property="Content" Value="*"/>
19                         </Trigger>
20                         <Trigger Property="IsPressed" Value="True">
21                             <Setter Property="Opacity" Value="0.6"/>
22                             <!--针对具体控件设置TargetName,设置bg的背景颜色为红色-->
23                             <Setter TargetName="bg" Property="Background"  Value="Red"/>
24                         </Trigger>
25                     </ControlTemplate.Triggers>
26                 </ControlTemplate>
27 
28             </Setter.Value>
29         </Setter>
30         
31     </Style>