zoukankan      html  css  js  c++  java
  • WPF 触发器例子

    WPF的触发器很强大,这里简单附上触发器的一个小例子,分别用XMAL和CS代码来实现一个功能,鼠标悬停在button上时改变字体颜色

    1.XMAL代码如下:

    <Window x:Class="wpf触发器.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="350" Width="525">
        <Grid Name="grid" >
            <Button x:Name="btnHello" Content="clickMe" Height="24" Width="60"  >
                <Button.Style>
                    <Style TargetType="Button">
                        <Style.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter Property="Foreground" Value="Blue"></Setter>
                                <Setter Property="Background" Value="#00ff00"></Setter>
                            </Trigger>
                        </Style.Triggers>
                    </Style>
                </Button.Style>
            </Button>
        </Grid>
    </Window>


    Cs代码如下:
            public MainWindow()
            {
                InitializeComponent();
                Button btn = new Button() { Width =100,Height=50,Content="overme"};

                //实例style,参数带上控件类型
                Style m_style = new Style( typeof(Button));

                //实例trigger,并加上触发的property和value
                Trigger trigger = new Trigger();
                trigger.Property = IsMouseOverProperty;
                trigger.Value = true;

                //当满足trigger设定的条件时,要应用的属性值
                trigger.Setters.Add(new Setter(ForegroundProperty, Brushes.Red));

                m_style.Triggers.Add(trigger);
                btn.Style = m_style;
                grid.Children.Add(btn); //grid就是外面的Grid
            }
       


  • 相关阅读:
    android29
    android28
    android27
    android26
    Dynamics CRM2011 MspInstallAction failed when installing an Update Rollup
    Dynamics CRM Import Solution Attribute Display Name description is null or empty
    The service cannot be activated because it does not support ASP.NET compatibility
    IIS部署WCF报 无法读取配置节“protocolMapping”,因为它缺少节声明
    Unable to access the IIS metabase.You do not have sufficient privilege
    LM算法与非线性最小二乘问题
  • 原文地址:https://www.cnblogs.com/kevinWu7/p/10163553.html
Copyright © 2011-2022 走看看