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
            }
       


  • 相关阅读:
    Bootstrap+Angularjs自制弹框
    【2019年04月22日】A股最便宜的股票
    沪深300指数的跟踪基金排名
    【2019年04月10日】股票的滚动市盈率PE最低排名
    【2019年04月09日】A股净资产收益率ROE最高排名
    基金 、社保和QFII等机构的重仓股排名评测
    【2019年04月04日】股市指数估值排名
    【2019年04月03日】A股最便宜的股票
    净资产收益率ROE连续3年超过15%的股票排名
    A股滚动净利润增速最高排名
  • 原文地址:https://www.cnblogs.com/kevinWu7/p/10163553.html
Copyright © 2011-2022 走看看