首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 媒体动画 > CAD教程 >

WPF属性触发器不发生效果

2013-01-05 
WPF属性触发器不产生效果我XAML页面设置这样一个触发器,为什么没有起到效果呢Window x:ClassProDemo.Ma

WPF属性触发器不产生效果
我XAML页面设置这样一个触发器,为什么没有起到效果呢


<Window x:Class="ProDemo.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" Name="FrmMain">
    <Window.Resources>
        <Style TargetType="{x:Type Button}">
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Background" Value="Black"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    <Grid>
        <Button Content="Button" Height="43" Margin="12,48,356,218" Name="btn" Width="135" Click="btn_Click" />
        <TextBox Height="23" HorizontalAlignment="Left" Margin="12,20,0,0" Name="txt" VerticalAlignment="Top" Width="479" />
    </Grid>
</Window>

鼠标移到按钮上面,应该是要变成橙色才对,但是一点反应都没有,请问是咋回事呀
[解决办法]
 <Style TargetType="{x:Type RibbonLib:RibbonButton}">
        <Setter Property="Focusable" Value="False"></Setter>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type RibbonLib:RibbonButton}">
                    <Grid Cursor="Hand">
                        <!--背景色-->
                        <Border x:Name="back" Opacity="0.8" CornerRadius="3" BitmapEffect="{DynamicResource {ComponentResourceKey RibbonLib:Skins, RibbonActiveButtonBitmapEffect}}" Background="{DynamicResource {ComponentResourceKey RibbonLib:Skins, RibbonActiveButtonBrush}}">
                            <!--前景色及边框-->
                            <Border x:Name="fore" BorderThickness="1" CornerRadius="3" BorderBrush="{DynamicResource {ComponentResourceKey RibbonLib:Skins, RibbonActiveButtonBorderBrush}}">
                                <Border.Background>


                                    <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
                                        <GradientBrush.GradientStops>
                                            <GradientStopCollection>
                                                <GradientStop Color="#6FFF" Offset="0.5"/>
                                                <GradientStop Color="#1111" Offset="0.51"/>
                                            </GradientStopCollection>
                                        </GradientBrush.GradientStops>
                                    </LinearGradientBrush>
                                </Border.Background>
                                <!--按钮内容-->
                                <StackPanel x:Name="CenterPanel" HorizontalAlignment="Center" VerticalAlignment="Center" Orientation="Horizontal">
                                    <Image x:Name="CenterImage" HorizontalAlignment="Center" VerticalAlignment="Center" Stretch="Fill" Source="{TemplateBinding LargeImage}"/>
                                    <TextBlock x:Name="CenterBlock" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="8,0,0,0" Text="{TemplateBinding  Content}" Foreground="{DynamicResource {ComponentResourceKey RibbonLib:Skins, RibbonForgroundBrush}}"/>


                                </StackPanel>
                            </Border>
                        </Border>
                    </Grid>
                    <!--触发器-->
                    <ControlTemplate.Triggers>
                        <!--鼠标移入移出-->
                        <Trigger Property="IsMouseOver" Value="True">
                            <Trigger.EnterActions>
                                <BeginStoryboard>
                                    <Storyboard>
                                        <DoubleAnimation To="6" Duration="0:0:0.2" Storyboard.TargetName="back" Storyboard.TargetProperty="(Border.BitmapEffect).(OuterGlowBitmapEffect.GlowSize)" />
                                        <ColorAnimation To="#AFFF" BeginTime="0:0:0.2" Duration="0:0:0.2" Storyboard.TargetName="fore" Storyboard.TargetProperty="(Border.Background).(LinearGradientBrush.GradientStops)[0].(GradientStop.Color)" />
                                        <ColorAnimation To="#3FFF" BeginTime="0:0:0.2" Duration="0:0:0.2" Storyboard.TargetName="fore" Storyboard.TargetProperty="(Border.Background).(LinearGradientBrush.GradientStops)[1].(GradientStop.Color)" />
                                    </Storyboard>
                                </BeginStoryboard>
                            </Trigger.EnterActions>


                            <Trigger.ExitActions>
                                <BeginStoryboard>
                                    <Storyboard>
                                        <DoubleAnimation Duration="0:0:0.2" Storyboard.TargetName="back" Storyboard.TargetProperty="(Border.BitmapEffect).(OuterGlowBitmapEffect.GlowSize)" />
                                        <ColorAnimation Duration="0:0:0.2" Storyboard.TargetName="fore" Storyboard.TargetProperty="(Border.Background).(LinearGradientBrush.GradientStops)[0].(GradientStop.Color)" />
                                        <ColorAnimation Duration="0:0:0.2" Storyboard.TargetName="fore" Storyboard.TargetProperty="(Border.Background).(LinearGradientBrush.GradientStops)[1].(GradientStop.Color)" />
                                    </Storyboard>
                                </BeginStoryboard>
                            </Trigger.ExitActions>
                        </Trigger>
                    </ControlTemplate.Triggers>

                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
[解决办法]
在WPF中,触发器仅有效于样式和模板中,并且需要EventTriggers支持。
如果需要将触发器用于样式和模板外,可以将触发器套在一下代码:


<Control>
    <Control.Template>
        <ControlTemplate>
            <!-- 触发器代码 -->
        </ControlTemplate>
    </Control.Template>
</Control>


最常规的使用方法如下:


<Style x:Key="styleWithTrigger" TargetType="Rectangle">


    <Setter Property="Fill" Value="LightGreen" />
    <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="Fill" Value="Red" />
        </Trigger>
    </Style.Triggers>
</Style>
 
<Rectangle Style="{StaticResource styleWithTrigger}"></Rectangle>

热点排行