3

我为我的 Silverlight 按钮创建了一个样式模板,设法创建了圆角和悬停状态,可以毫无问题地更改大部分样式,但是...

我不知道如何使Foreground颜色改变hover

请参阅下面的代码,我遇到问题的部分目前已被注释掉。

    <Style TargetType="Button" >
        <Setter x:Name="myFontColor" Property="Foreground" Value="#000000"/>
        <Setter Property="FontSize" Value="11"/>
        <Setter Property="FontStyle" Value="Normal"/>
        <Setter Property="FontFamily" Value="Arial"/>
        <Setter Property="FontWeight" Value="Normal"/>
        <Setter Property="Padding" Value="0"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate>
                    <Grid Height="28">
                        <Border x:Name="myBorder" BorderBrush="#C4C4C4" BorderThickness="1" CornerRadius="5">
                            <Rectangle x:Name="BackgroundGradient" RadiusX="5" RadiusY="5">
                                <Rectangle.Fill>
                                    <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
                                        <GradientStop x:Name="GradientStop1" Color="#FDFDFD" Offset="0" />
                                        <GradientStop x:Name="GradientStop2" Color="#D6D6D6" Offset="1" />
                                    </LinearGradientBrush>
                                </Rectangle.Fill>
                            </Rectangle>
                        </Border>
                        <ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center" />
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommomStates">
                                <VisualState x:Name="Normal" />
                                <VisualState x:Name="MouseOver">
                                    <Storyboard>
                                        <ColorAnimation 
                                                    Storyboard.TargetName="GradientStop1"
                                                    Storyboard.TargetProperty="Color"
                                                    From="#FDFDFD" To="#0A284B" 
                                                    Duration="0"
                                                    />
                                        <ColorAnimation 
                                                    Storyboard.TargetName="GradientStop2"
                                                    Storyboard.TargetProperty="Color"
                                                    From="#D6D6D6" To="#135887"
                                                    Duration="0"
                                                    />

                                        <ColorAnimation 
                                                    Storyboard.TargetName="myBorder" 
                                                    Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)"
                                                    From="#C4C4C4" To="#000000" 
                                                    Duration="0"
                                            />
                                        <!--<ColorAnimation
                                                    Storyboard.TargetName="myFontColor" 
                                                    Storyboard.TargetProperty="Foreground"
                                                    From="#000000" To="#FFFFFF" 
                                                    Duration="0"
                                            />-->
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

作为 Silverlight 的新手,我希望这只是一个针对正确元素和样式的简单问题。

我该如何去Foreground改变颜色hover

提前致谢

4

4 回答 4

8

首先,在ContentPresenter里面放一个ContentControl(这有 Foreground 属性)然后你可以改变它,就像你对背景所做的那样:

<Style TargetType="Button" >
    <Setter x:Name="myFontColor" Property="Foreground" Value="#000000"/>
    <Setter Property="FontSize" Value="11"/>
    <Setter Property="FontStyle" Value="Normal"/>
    <Setter Property="FontFamily" Value="Arial"/>
    <Setter Property="FontWeight" Value="Normal"/>
    <Setter Property="Padding" Value="0"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate>
                <Grid Height="28">
                    <Border x:Name="myBorder" BorderBrush="#C4C4C4" BorderThickness="1" CornerRadius="5">
                        <Rectangle x:Name="BackgroundGradient" RadiusX="5" RadiusY="5">
                            <Rectangle.Fill>
                                <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
                                    <GradientStop x:Name="GradientStop1" Color="#FDFDFD" Offset="0" />
                                    <GradientStop x:Name="GradientStop2" Color="#D6D6D6" Offset="1" />
                                </LinearGradientBrush>
                            </Rectangle.Fill>
                        </Rectangle>
                    </Border>
                    <!-- CONTENT CONTROL HERE -->
                    <ContentControl Name="content" VerticalAlignment="Center" HorizontalAlignment="Center">
                    <ContentPresenter  />
                    </ContentControl>
                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup x:Name="CommomStates">
                            <VisualState x:Name="Normal" />
                            <VisualState x:Name="MouseOver">
                                <Storyboard>
                                    <ColorAnimation 
                                        Storyboard.TargetName="GradientStop1"
                                        Storyboard.TargetProperty="Color"
                                        From="#FDFDFD" To="#0A284B" 
                                        Duration="0"
                                        />
                                    <ColorAnimation 
                                        Storyboard.TargetName="GradientStop2"
                                        Storyboard.TargetProperty="Color"
                                        From="#D6D6D6" To="#135887"
                                        Duration="0"
                                        />

                                    <ColorAnimation 
                                        Storyboard.TargetName="myBorder" 
                                        Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)"
                                        From="#C4C4C4" To="#000000" 
                                        Duration="0"
                                />


                                   <!-- ALTERNATIVE WAY 

                                  <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Foreground)" Storyboard.TargetName="content">
                                                    <DiscreteObjectKeyFrame KeyTime="0" Value="White" />
                                                </ObjectAnimationUsingKeyFrames>-->


                                    <ColorAnimation
                                        Storyboard.TargetName="content" 
                                        Storyboard.TargetProperty="(UIElement.Foreground).(SolidColorBrush.Color)"
                                        From="#000000" To="#FFFFFF" 
                                        Duration="0"
                                />
                                </Storyboard>
                            </VisualState>
                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
于 2012-07-13T07:17:59.563 回答
1

您可以通过添加事件 MouseEnter 和 MouseLeave 并在事件中更改颜色来做到这一点

例子:

<Button x:Name="myButton" 
    Content="Button" 
    MouseEnter="myButton_MouseEnter" 
    MouseLeave="myButton_MouseLeave"/>

在 C# 类中,你必须有方法

    private void myButton_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
    {
        myButton.Foreground=new SolidColorBrush(Colors.Red);
    }

    private void myButton_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
    {
        myButton.Foreground=new SolidColorBrush(Colors.White);
    }

希望,这就是你要找的。

于 2012-07-13T07:12:57.660 回答
1

使用 Blend 来设计任何控件。使用它,您可以轻松创建任何您想要的样式

只需通过创建一个按钮

于 2012-07-13T07:13:17.920 回答
1

试试这个:

<Style x:Key="CustomButtonStyle" TargetType="Button">
    <Setter Property="Background" Value="#FF1F3B53"/>
    <Setter Property="Foreground" Value="#FF000000"/>
    <Setter Property="Padding" Value="3"/>
    <Setter Property="BorderThickness" Value="0"/>
    <Setter Property="BorderBrush">
        <Setter.Value>
            <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                <GradientStop Color="#FFA3AEB9" Offset="0"/>
                <GradientStop Color="#FF8399A9" Offset="0.375"/>
                <GradientStop Color="#FF718597" Offset="0.375"/>
                <GradientStop Color="#FF617584" Offset="1"/>
            </LinearGradientBrush>
        </Setter.Value>
    </Setter>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <Grid>
                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup x:Name="CommonStates">
                            <VisualState x:Name="Normal">
                                <Storyboard>
                                    <DoubleAnimation Duration="0" Storyboard.TargetName="BackgroundAnimation" Storyboard.TargetProperty="Opacity" To="1"/>
                                    <ColorAnimation Duration="0" Storyboard.TargetName="BackgroundGradient" Storyboard.TargetProperty="(Rectangle.Fill).(GradientBrush.GradientStops)[0].(GradientStop.Color)" To="#FF474747"/> <!--change this color code if you wish-->
                                    <ColorAnimation Duration="0" Storyboard.TargetName="BackgroundGradient" Storyboard.TargetProperty="(Rectangle.Fill).(GradientBrush.GradientStops)[1].(GradientStop.Color)" To="#FF474747"/> <!--change this color code if you wish-->
                                    <ColorAnimation Duration="0" Storyboard.TargetName="BackgroundGradient" Storyboard.TargetProperty="(Rectangle.Fill).(GradientBrush.GradientStops)[2].(GradientStop.Color)" To="#FF474747"/> <!--change this color code if you wish-->
                                    <ColorAnimation Duration="0" Storyboard.TargetName="BackgroundGradient" Storyboard.TargetProperty="(Rectangle.Fill).(GradientBrush.GradientStops)[3].(GradientStop.Color)" To="#FF474747"/> <!--change this color code if you wish-->
                                </Storyboard>
                            </VisualState>
                            <VisualState x:Name="MouseOver">
                                <Storyboard>
                                    <DoubleAnimation Duration="0" Storyboard.TargetName="BackgroundAnimation" Storyboard.TargetProperty="Opacity" To="1"/>
                                    <ColorAnimation Duration="0" Storyboard.TargetName="BackgroundGradient" Storyboard.TargetProperty="(Rectangle.Fill).(GradientBrush.GradientStops)[1].(GradientStop.Color)" To="DarkGray"/>
                                    <ColorAnimation Duration="0" Storyboard.TargetName="BackgroundGradient" Storyboard.TargetProperty="(Rectangle.Fill).(GradientBrush.GradientStops)[2].(GradientStop.Color)" To="DarkGray"/>
                                    <ColorAnimation Duration="0" Storyboard.TargetName="BackgroundGradient" Storyboard.TargetProperty="(Rectangle.Fill).(GradientBrush.GradientStops)[3].(GradientStop.Color)" To="DarkGray"/>
                                </Storyboard>
                            </VisualState>
                            <VisualState x:Name="Pressed">
                                <Storyboard>
                                    <ColorAnimation Duration="0" Storyboard.TargetName="Background" Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)" To="DarkGray"/>
                                    <DoubleAnimation Duration="0" Storyboard.TargetName="BackgroundAnimation" Storyboard.TargetProperty="Opacity" To="1"/>
                                    <ColorAnimation Duration="0" Storyboard.TargetName="BackgroundGradient" Storyboard.TargetProperty="(Rectangle.Fill).(GradientBrush.GradientStops)[0].(GradientStop.Color)" To="DarkGray"/>
                                    <ColorAnimation Duration="0" Storyboard.TargetName="BackgroundGradient" Storyboard.TargetProperty="(Rectangle.Fill).(GradientBrush.GradientStops)[1].(GradientStop.Color)" To="DarkGray"/>
                                    <ColorAnimation Duration="0" Storyboard.TargetName="BackgroundGradient" Storyboard.TargetProperty="(Rectangle.Fill).(GradientBrush.GradientStops)[2].(GradientStop.Color)" To="DarkGray"/>
                                    <ColorAnimation Duration="0" Storyboard.TargetName="BackgroundGradient" Storyboard.TargetProperty="(Rectangle.Fill).(GradientBrush.GradientStops)[3].(GradientStop.Color)" To="DarkGray"/>
                                </Storyboard>
                            </VisualState>
                            <VisualState x:Name="Disabled">
                                <Storyboard>
                                    <DoubleAnimation Duration="0" Storyboard.TargetName="DisabledVisualElement" Storyboard.TargetProperty="Opacity" To=".55"/>
                                </Storyboard>
                            </VisualState>
                        </VisualStateGroup>
                        <VisualStateGroup x:Name="FocusStates">
                            <VisualState x:Name="Focused">
                                <Storyboard>
                                    <DoubleAnimation Duration="0" Storyboard.TargetName="FocusVisualElement" Storyboard.TargetProperty="Opacity" To="1"/>
                                </Storyboard>
                            </VisualState>
                            <VisualState x:Name="Unfocused" />
                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>
                    <Border x:Name="Background" CornerRadius="3" Background="White" BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding BorderBrush}">
                        <Grid Background="{TemplateBinding Background}"  Margin="1">
                            <Border Opacity="0"  x:Name="BackgroundAnimation" Background="#FF448DCA" />
                            <Rectangle x:Name="BackgroundGradient" >
                                <Rectangle.Fill>
                                    <LinearGradientBrush StartPoint=".7,0" EndPoint=".7,1">
                                        <GradientStop Color="#A9A9A9" Offset="0" />
                                        <GradientStop Color="#A5A5A5" Offset="0.375" />
                                        <GradientStop Color="#A3A3A3" Offset="0.625" />
                                        <GradientStop Color="#A0A0A0" Offset="1" />
                                    </LinearGradientBrush>
                                </Rectangle.Fill>
                            </Rectangle>
                        </Grid>
                    </Border>
                    <ContentPresenter
                          x:Name="contentPresenter"
                          Content="{TemplateBinding Content}"
                          ContentTemplate="{TemplateBinding ContentTemplate}"
                          VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                          HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                          Margin="{TemplateBinding Padding}"/>
                    <Rectangle x:Name="DisabledVisualElement" RadiusX="3" RadiusY="3" Fill="#A9A9A9" Opacity="0" IsHitTestVisible="false" />
                    <Rectangle x:Name="FocusVisualElement" RadiusX="2" RadiusY="2" Margin="1" Stroke="#FF6DBDD1" StrokeThickness="1" Opacity="0" IsHitTestVisible="false" />
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
于 2012-07-13T07:35:37.960 回答