在运行时更改大小按钮(wpf)

时间:2011-08-02 16:22:42

标签: wpf xaml styles

为什么大小在IsMouseOver中没有变化?

                   <Style x:Key="GlassButton" TargetType="{x:Type Button}">
    <Setter Property="SnapsToDevicePixels" Value="True"/>
    <Setter Property="OverridesDefaultStyle" Value="True"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <Grid>
                    <!-- Background Layer -->
                    <Ellipse Fill="{TemplateBinding Background}"/>
                    <!-- Refraction Layer -->
                    <Ellipse x:Name="RefractionLayer">
                        <Ellipse.Fill>
                            <RadialGradientBrush GradientOrigin="0.496,1.052">
                                <RadialGradientBrush.RelativeTransform>
                                    <TransformGroup>
                                        <ScaleTransform CenterX="0.5" CenterY="0.5" ScaleX="1.5" ScaleY="1.5"/>
                                        <TranslateTransform X="0.02" Y="0.3"/>
                                    </TransformGroup>
                                </RadialGradientBrush.RelativeTransform>
                            </RadialGradientBrush>
                        </Ellipse.Fill>
                    </Ellipse>
                    <!-- Reflection Layer -->
                    <Path x:Name="ReflectionLayer" VerticalAlignment="Top" Stretch="Fill">
                        <Path.RenderTransform>
                            <ScaleTransform ScaleY="0.5" />
                        </Path.RenderTransform>
                        <Path.Data>
                            <PathGeometry>
                                <PathFigure IsClosed="True" StartPoint="98.999,45.499">
                                    <BezierSegment Point1="98.999,54.170" Point2="89.046,52.258" Point3="85.502,51.029"/>
                                    <BezierSegment
                            IsSmoothJoin="True"
                            Point1="75.860,47.685"
                            Point2="69.111,45.196"
                            Point3="50.167,45.196"/>
                                    <BezierSegment Point1="30.805,45.196" Point2="20.173,47.741" Point3="10.665,51.363"/>
                                    <BezierSegment
                            IsSmoothJoin="True"
                            Point1="7.469,52.580"
                            Point2="1.000,53.252"
                            Point3="1.000,44.999"/>
                                    <BezierSegment Point1="1.000,39.510" Point2="0.884,39.227" Point3="2.519,34.286"/>
                                    <BezierSegment
                            IsSmoothJoin="True"
                            Point1="9.106,14.370"
                            Point2="27.875,0"
                            Point3="50,0"/>
                                    <BezierSegment Point1="72.198,0" Point2="91.018,14.466" Point3="97.546,34.485"/>
                                    <BezierSegment
                            IsSmoothJoin="True"
                            Point1="99.139,39.369"
                            Point2="98.999,40.084"
                            Point3="98.999,45.499"/>
                                </PathFigure>
                            </PathGeometry>
                        </Path.Data>
                        <Path.Fill>
                            <RadialGradientBrush GradientOrigin="0.498,0.526">
                                <RadialGradientBrush.RelativeTransform>
                                    <TransformGroup>
                                        <ScaleTransform
                               CenterX="0.5"
                               CenterY="0.5"
                               ScaleX="1"
                               ScaleY="1.997"/>
                                        <TranslateTransform X="0" Y="0.5"/>
                                    </TransformGroup>
                                </RadialGradientBrush.RelativeTransform>
                            </RadialGradientBrush>
                        </Path.Fill>
                    </Path>
                    <!-- ContentPresenter -->
                    <ContentPresenter Margin="0,2,0,0" HorizontalAlignment="Center" VerticalAlignment="Center"/>


                </Grid>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="Width" Value="200"/>
                        <Setter Property="Height" Value="200"/>
                        <Setter TargetName="RefractionLayer" Property="Fill">
                            <Setter.Value>
                                <RadialGradientBrush GradientOrigin="0.496,1.052">
                                    <RadialGradientBrush.RelativeTransform>
                                        <TransformGroup>
                                            <ScaleTransform
                                  CenterX="0.5"
                                  CenterY="0.5"
                                  ScaleX="1.5"
                                  ScaleY="1.5"/>
                                            <TranslateTransform  X="0.02" Y="0.3"/>
                                        </TransformGroup>
                                    </RadialGradientBrush.RelativeTransform>
                                    <GradientStop  Offset="1" Color="#00000000"/>
                                    <GradientStop Offset="0.45" Color="#FFFFFFFF"/>
                                </RadialGradientBrush>
                            </Setter.Value>
                        </Setter>
                        <Setter TargetName="ReflectionLayer" Property="Fill">
                            <Setter.Value>
                                <RadialGradientBrush GradientOrigin="0.498,0.526">
                                    <RadialGradientBrush.RelativeTransform>
                                        <TransformGroup>
                                            <ScaleTransform
                                  CenterX="0.5"
                                  CenterY="0.5"
                                  ScaleX="1"
                                  ScaleY="1.997"/>
                                            <TranslateTransform X="0" Y="0.5"/>
                                        </TransformGroup>
                                    </RadialGradientBrush.RelativeTransform>
                                    <GradientStop Offset="1" Color="#FFFFFFFF"/>
                                    <GradientStop Offset="0.85" Color="#BBFFFFFF"/>
                                    <GradientStop Offset="0" Color="#00000000"/>
                                </RadialGradientBrush>
                            </Setter.Value>


                        </Setter>
                    </Trigger>
                    <Trigger Property="IsPressed" Value="True">
                        <Setter TargetName="RefractionLayer" Property="Fill">
                            <Setter.Value>
                                <RadialGradientBrush GradientOrigin="0.496,1.052">
                                    <RadialGradientBrush.RelativeTransform>
                                        <TransformGroup>
                                            <ScaleTransform
                                  CenterX="0.5"
                                  CenterY="0.5"
                                  ScaleX="1.5"
                                  ScaleY="1.5"/>
                                            <TranslateTransform X="0.02" Y="0.3"/>
                                        </TransformGroup>
                                    </RadialGradientBrush.RelativeTransform>

                                </RadialGradientBrush>
                            </Setter.Value>
                        </Setter>
                        <Setter TargetName="ReflectionLayer" Property="Fill">
                            <Setter.Value>
                                <RadialGradientBrush GradientOrigin="0.498,0.526">
                                    <RadialGradientBrush.RelativeTransform>
                                        <TransformGroup>
                                            <ScaleTransform
                                  CenterX="0.5"
                                  CenterY="0.5"
                                  ScaleX="1"
                                  ScaleY="1.997"/>
                                            <TranslateTransform X="0" Y="0.5"/>
                                        </TransformGroup>
                                    </RadialGradientBrush.RelativeTransform>
                                </RadialGradientBrush>
                            </Setter.Value>
                        </Setter>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

示例用法:

 <Button Style="{DynamicResource GlassButton}"  Content="Button" Height="23" HorizontalAlignment="Left" Margin="534,262,0,0" Name="button1" VerticalAlignment="Top" Width="75" />

1 个答案:

答案 0 :(得分:2)

您有一个属性设置优先级问题。请参阅WPFTutorial.net上的优先顺序表(请参阅值解析策略部分)。

删除Height上的WidthButton设置,如果您需要初始尺寸,请将这些设置符添加到您的样式中:

<Setter Property="Height" Value="23"/>
<Setter Property="Width" Value="75"/>

并且,请注意Blend为您添加尺寸....