none
炫酷按钮样式怎么添加音效触发器啊? RRS feed

  • 问题

  • 下面是个炫酷按钮样式,想在其中添加音效,鼠标 移动到按钮区域和点击的音效,不知道怎么添加SoundPlayAction,请大家指导下!

      <Style        TargetType="{x:Type Button}">
            <Setter Property="Background">
                <Setter.Value>
                    <RadialGradientBrush>
                        <GradientBrush.GradientStops>
                            <GradientStopCollection>
                                <GradientStop Color="black"
                                Offset="0" />
                                <GradientStop Color="black"
                                Offset="1" />
                            </GradientStopCollection>
                        </GradientBrush.GradientStops>
                    </RadialGradientBrush>
                </Setter.Value>
            </Setter>
            <Setter Property="Height"
                Value="40" />
            <Setter Property="Foreground"
                Value="{DynamicResource buttonForecolor}" />
            <Setter Property="Grid.Row"
                Value="2" />
            <Setter Property="Grid.Column"
                Value="3" />
            <Setter Property="Content"
                Value="Forgot your password?" />
            <Setter Property="ContentTemplate"
                Value="{DynamicResource myContentTemplate}" />
            <Setter Property="Margin"
                Value="3" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Grid>
                            <Rectangle x:Name="GelBackground"
                           Opacity="1"
                           RadiusX="9"
                           RadiusY="9"
                           Fill="{TemplateBinding  Background}"
                           StrokeThickness="0.35">
                                <Rectangle.Stroke>
                                    <LinearGradientBrush StartPoint="0,0"
                                         EndPoint="0,1">
                                        <GradientBrush.GradientStops>
                                            <GradientStopCollection>
                                                <GradientStop Color="white"
                                        Offset="0" />
                                                <GradientStop Color="#666666"
                                        Offset="1" />
                                            </GradientStopCollection>
                                        </GradientBrush.GradientStops>
                                    </LinearGradientBrush>
                                </Rectangle.Stroke>
                            </Rectangle>
                            <Rectangle x:Name="GelShine"
                           Margin="2,2,2,0"
                           VerticalAlignment="top"
                           RadiusX="6"
                           RadiusY="6"
                           Opacity="1"
                           Stroke="transparent"
                           Height="15px">
                                <Rectangle.Fill>
                                    <LinearGradientBrush StartPoint="0,0"
                                         EndPoint="0,1">
                                        <GradientBrush.GradientStops>
                                            <GradientStopCollection>
                                                <GradientStop Color="#ccffffff"
                                        Offset="0" />
                                                <GradientStop Color="transparent"
                                        Offset="1" />
                                            </GradientStopCollection>
                                        </GradientBrush.GradientStops>
                                    </LinearGradientBrush>
                                </Rectangle.Fill>
                            </Rectangle>
                            <ContentPresenter x:Name="GelButtonContent"
                                  VerticalAlignment="center"
                                  HorizontalAlignment="center"
                                  Content="{TemplateBinding  Content}" />
                        </Grid>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver"
                         Value="true">
                                <Setter Property="Rectangle.Fill"
                          TargetName="GelBackground">
                                    <Setter.Value>
                                        <RadialGradientBrush>
                                            <GradientBrush.GradientStops>
                                                <GradientStopCollection>
                                                    <GradientStop Color="lime"
                                          Offset="0" />
                                                    <GradientStop Color="DarkGreen"
                                          Offset="1" />
                                                </GradientStopCollection>
                                            </GradientBrush.GradientStops>
                                        </RadialGradientBrush>
                                    </Setter.Value>
                                </Setter>
                            </Trigger>
                            <Trigger Property="IsPressed"
                         Value="true">
                                <Setter Property="Rectangle.Fill"
                          TargetName="GelBackground">
                                    <Setter.Value>
                                        <RadialGradientBrush>
                                            <GradientBrush.GradientStops>
                                                <GradientStopCollection>
                                                    <GradientStop Color="#ffcc00"
                                          Offset="0" />
                                                    <GradientStop Color="#cc9900"
                                          Offset="1" />
                                                </GradientStopCollection>
                                            </GradientBrush.GradientStops>
                                        </RadialGradientBrush>
                                    </Setter.Value>
                                </Setter>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Style.Triggers>
                <Trigger Property="IsMouseOver"                Value="true">
                    <Setter Property="Foreground"           Value="black " />
                </Trigger>
                <Trigger Property="IsPressed"
                   Value="true">
                    <Setter Property="Foreground"
                    Value="black " />
                </Trigger>
            </Style.Triggers>
        </Style>

    2011年8月22日 3:36

答案

  • 事件加在上面资源里面?还是BUTTON产生的地方?我需要所有BUTTON都应用这个,如果在BUTTON产生的地方这样,那好像不合适哦


    你可以用 EventSetter在样式中来统一设置一个事件的处理函数

            <Button >
                <Button.Style>
                    <Style TargetType="{x:Type Button}">
                        <EventSetter Event="MouseEnter" Handler="...."/>
                    </Style>
                </Button.Style>
            </Button>

     

    或者我还是建议你把这个按钮设计成CustomControl,这样你可以重载 OnMouseEnter OnMouseDown等方法 来实现你要的功能。

    Sincerely, 


    Bob Bao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    2011年8月22日 16:47
    版主

全部回复

  • 你可以做在代码里。

    鼠标进入是MouseEnter

    点击则是Click,或者使用MouseDown事件

    2011年8月22日 8:28
  • 事件加在上面资源里面?还是BUTTON产生的地方?我需要所有BUTTON都应用这个,如果在BUTTON产生的地方这样,那好像不合适哦

    2011年8月22日 9:02
  • 事件加在上面资源里面?还是BUTTON产生的地方?我需要所有BUTTON都应用这个,如果在BUTTON产生的地方这样,那好像不合适哦


    你可以用 EventSetter在样式中来统一设置一个事件的处理函数

            <Button >
                <Button.Style>
                    <Style TargetType="{x:Type Button}">
                        <EventSetter Event="MouseEnter" Handler="...."/>
                    </Style>
                </Button.Style>
            </Button>

     

    或者我还是建议你把这个按钮设计成CustomControl,这样你可以重载 OnMouseEnter OnMouseDown等方法 来实现你要的功能。

    Sincerely, 


    Bob Bao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    2011年8月22日 16:47
    版主
  • 你好,金钱豹

    我暂时将这个回复最为答案,如果你有任何新的发现或者不同意见的话,敬请取消这个标记。

    我们将非常感谢您对我们的回馈,并和您一起继续解决问题。


    Annabella Luo [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    2011年8月29日 3:47
    版主