WPF触发器变化相应控件外观
创始人
2024-06-17 20:11:00
0

WPF开发工具的主要用途就是实现图形界面的显示功能。在学习的过程中我们有许多需要深入研究的内容。比如,除了定义控件的默认外观外,也许我们想还定义当外界刺激我们的控件时,控件外观做出相应的变化,这是我们需要触发器。参考以下代码: #t#

  1. < Button Content="test btn" 
    Grid.Column="1" Grid.ColumnSpan="1" 
    Grid.Row="1" Grid.RowSpan="1" >   
  2. < Button.Template>   
  3. < ControlTemplate>   
  4. < !--定义视觉树-->   
  5. < Grid>   
  6. < Ellipse Name="faceEllipse" Width="
    {TemplateBinding Button.Width}"
     Height="
    {TemplateBinding Control.Height}"
     
    Fill="{TemplateBinding Button.Background}"/>   
  7. < TextBlock Name="txtBlock" Margin="
    {TemplateBinding Button.Padding}"
     
    VerticalAlignment="Center" Horizontal
    Alignment
    ="Center" Text="{Template
    Binding Button.Content}"
     />   
  8. < /Grid>   
  9. < !--定义视觉树_end-->   
  10. < !--定义触发器-->   
  11. < ControlTemplate.Triggers>   
  12. < Trigger Property="Button.
    IsMouseOver"
     Value="True"> 
  13. < Setter Property="Button.
    Foreground"
     Value="Red" /> 
  14. < /Trigger>   
  15. < /ControlTemplate.Triggers>   
  16. < !--定义触发器_End-->   
  17. < /ControlTemplate>   
  18. < /Button.Template>   
  19. < /Button> 

在上面的WPF触发器代码中注意到< ControlTemplate.Triggers>... < /ControlTemplate.Triggers>之间的部分,我们定义了触发器 < Trigger Property="Button.IsMouseOver" Value="True">,其表示当我们Button的IsMouseIOver属性变成True时,将使用设置器< Setter Property="Button.Foreground" Value="Red" /> 来将Button的Foreground属性设置为Red。这里有一个隐含的意思是:当Button的IsMouseIOver属性变成False时,设置器中设置的属性将回复原值。

你可以粘贴以下代码到XamlPad查看效果:

 

  1. < Window xmlns="http://schemas.
    microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com
    /winfx/2006/xaml" Title="ControlTemplateTest" 
    Height="300" Width="300" >   
  2. < Grid ShowGridLines="True">   
  3. < Grid.ColumnDefinitions>   
  4. < ColumnDefinition Width="0.2*"/>   
  5. < ColumnDefinition Width="0.6*"/>   
  6. < ColumnDefinition Width="0.2*"/> 
  7. < /Grid.ColumnDefinitions>   
  8. < Grid.RowDefinitions>   
  9. < RowDefinition Height="0.3*"/>   
  10. < RowDefinition Height="0.3*"/> 
  11. < RowDefinition Height="0.4*"/> 
  12. < /Grid.RowDefinitions>   
  13. < Button Content="test btn" 
    Grid.Column="1" Grid.ColumnSpan="1" 
    Grid.Row="1" Grid.RowSpan="1" >   
  14. < Button.Template>   
  15. < ControlTemplate>   
  16. < !--定义视觉树-->   
  17. < Grid> < Ellipse Name="faceEllipse" Width="
    {TemplateBinding Button.Width}" Height="
    {TemplateBinding Control.Height}" 
    Fill="{TemplateBinding Button.Background}"/>   
  18. < TextBlock Name="txtBlock" Margin=
    "{TemplateBinding Button.Padding}"
     VerticalAlignment="Center" 
    HorizontalAlignment="Center" 
    Text="{TemplateBinding Button.Content}" />   
  19. < /Grid>   
  20. < !--定义视觉树_end-->   
  21. < !--定义触发器--> < ControlTemplate.Triggers>   
  22. < Trigger Property="Button.IsMouseOver" 
    Value="True">   
  23. < Setter Property="Button.Foreground" 
    Value="Red" />   
  24. < /Trigger> < /ControlTemplate.Triggers>   
  25. < !--定义触发器_End--> < /ControlTemplate>   
  26. < /Button.Template>   
  27. < /Button>   
  28. < /Grid>   
  29. < /Window>  

以上就是对WPF触发器的相关应用方法介绍。

相关内容

热门资讯

如何允许远程连接到MySQL数... [[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...
如何利用交换机和端口设置来管理... 在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...
施耐德电气数据中心整体解决方案... 近日,全球能效管理专家施耐德电气正式启动大型体验活动“能效中国行——2012卡车巡展”,作为该活动的...
Windows恶意软件20年“... 在Windows的早期年代,病毒游走于系统之间,偶尔删除文件(但被删除的文件几乎都是可恢复的),并弹...
20个非常棒的扁平设计免费资源 Apple设备的平面图标PSD免费平板UI 平板UI套件24平图标Freen平板UI套件PSD径向平...
德国电信门户网站可实时显示全球... 德国电信周三推出一个门户网站,直观地实时提供其安装在全球各地的传感器网络检测到的网络攻击状况。该网站...
为啥国人偏爱 Mybatis,... 关于 SQL 和 ORM 的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行...
《非诚勿扰》红人闫凤娇被曝厕所... 【51CTO.com 综合消息360安全专家提醒说,“闫凤娇”、“非诚勿扰”已经被黑客盯上成为了“木...