如何在触发器 isMouseOver 为真时放大按钮
How do I scale up button when trigger isMouseOver is true
刚开始学xaml所以对它几乎一无所知
我试图在用户 Overs on 按钮时将按钮放大到 1.2,但我找不到任何地方如何做到这一点
以下是xaml代码,如果有用的话,
<Window.Resources>
<SolidColorBrush x:Key="Button.Static.Background" Color="#FF124D91"/>
<SolidColorBrush x:Key="Button.Static.Border" Color="Transparent"/>
<SolidColorBrush x:Key="Button.Static.Foreground" Color="White"/>
<SolidColorBrush x:Key="Button.MouseOver.Background" Color="Transparent"/>
<SolidColorBrush x:Key="Button.MouseOver.Border" Color="#FF124D91"/>
<SolidColorBrush x:Key="Button.Pressed.Background" Color="#FFC4E5F6"/>
<SolidColorBrush x:Key="Button.Pressed.Border" Color="#FF124D91"/>
<SolidColorBrush x:Key="Button.Disabled.Background" Color="Transparent"/>
<SolidColorBrush x:Key="Button.Disabled.Border" Color="Transparent"/>
<SolidColorBrush x:Key="Button.Disabled.Foreground" Color="Transparent"/>
<Style x:Key="ButtonPressStyle" TargetType="{x:Type Button}">
<Setter Property="Background" Value="{StaticResource Button.Static.Background}"/>
<Setter Property="BorderBrush" Value="{StaticResource Button.Static.Border}"/>
<Setter Property="Foreground" Value="{DynamicResource Button.Static.Foreground}"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="Padding" Value="1"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border x:Name="border" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" SnapsToDevicePixels="true" CornerRadius="8">
<ContentPresenter x:Name="contentPresenter" Focusable="False" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsDefaulted" Value="true">
<Setter Property="BorderBrush" TargetName="border" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
<Setter Property="Foreground" Value="White"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Background" TargetName="border" Value="{StaticResource Button.MouseOver.Background}"/>
<Setter Property="BorderBrush" TargetName="border" Value="{StaticResource Button.MouseOver.Border}"/>
<Setter Property="Foreground" Value="Black"/>
<Setter Property="BorderThickness" Value="1.5"/>
</Trigger>
<Trigger Property="IsPressed" Value="true">
<Setter Property="Background" TargetName="border" Value="{StaticResource Button.Pressed.Background}"/>
<Setter Property="BorderBrush" TargetName="border" Value="{StaticResource Button.Pressed.Border}"/>
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Background" TargetName="border" Value="{StaticResource Button.Disabled.Background}"/>
<Setter Property="BorderBrush" TargetName="border" Value="{StaticResource Button.Disabled.Border}"/>
<Setter Property="TextElement.Foreground" TargetName="contentPresenter" Value="{StaticResource Button.Disabled.Foreground}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
按钮:
<Button x:Name="ButtonPress" Style="{DynamicResource ButtonPressStyle}" Margin="133,0,133,15" Content="Press" Height="50" VerticalAlignment="Bottom" Click="ButtonPress_Click"/>
此外,如果在 C# 中也有任何方法可以做到这一点,请教我怎么做,
感谢任何帮助。
将此添加到您的触发器中属性
<Setter Property="RenderTransformOrigin" Value="0.5, 0.5"/>
<Setter Property="RenderTransform">
<Setter.Value>
<ScaleTransform ScaleX="1.2" ScaleY="1.2"/>
</Setter.Value>
</Setter>
最终代码
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Background" TargetName="border" Value="{StaticResource Button.MouseOver.Background}"/>
<Setter Property="BorderBrush" TargetName="border" Value="{StaticResource Button.MouseOver.Border}"/>
<Setter Property="Foreground" Value="Black"/>
<Setter Property="BorderThickness" Value="1.5"/>
<Setter Property="RenderTransformOrigin" Value="0.5, 0.5"/>
<Setter Property="RenderTransform">
<Setter.Value>
<ScaleTransform ScaleX="1.2" ScaleY="1.2"/>
</Setter.Value>
</Setter>
</Trigger>
刚开始学xaml所以对它几乎一无所知
我试图在用户 Overs on 按钮时将按钮放大到 1.2,但我找不到任何地方如何做到这一点
以下是xaml代码,如果有用的话,
<Window.Resources>
<SolidColorBrush x:Key="Button.Static.Background" Color="#FF124D91"/>
<SolidColorBrush x:Key="Button.Static.Border" Color="Transparent"/>
<SolidColorBrush x:Key="Button.Static.Foreground" Color="White"/>
<SolidColorBrush x:Key="Button.MouseOver.Background" Color="Transparent"/>
<SolidColorBrush x:Key="Button.MouseOver.Border" Color="#FF124D91"/>
<SolidColorBrush x:Key="Button.Pressed.Background" Color="#FFC4E5F6"/>
<SolidColorBrush x:Key="Button.Pressed.Border" Color="#FF124D91"/>
<SolidColorBrush x:Key="Button.Disabled.Background" Color="Transparent"/>
<SolidColorBrush x:Key="Button.Disabled.Border" Color="Transparent"/>
<SolidColorBrush x:Key="Button.Disabled.Foreground" Color="Transparent"/>
<Style x:Key="ButtonPressStyle" TargetType="{x:Type Button}">
<Setter Property="Background" Value="{StaticResource Button.Static.Background}"/>
<Setter Property="BorderBrush" Value="{StaticResource Button.Static.Border}"/>
<Setter Property="Foreground" Value="{DynamicResource Button.Static.Foreground}"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="Padding" Value="1"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border x:Name="border" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" SnapsToDevicePixels="true" CornerRadius="8">
<ContentPresenter x:Name="contentPresenter" Focusable="False" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsDefaulted" Value="true">
<Setter Property="BorderBrush" TargetName="border" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
<Setter Property="Foreground" Value="White"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Background" TargetName="border" Value="{StaticResource Button.MouseOver.Background}"/>
<Setter Property="BorderBrush" TargetName="border" Value="{StaticResource Button.MouseOver.Border}"/>
<Setter Property="Foreground" Value="Black"/>
<Setter Property="BorderThickness" Value="1.5"/>
</Trigger>
<Trigger Property="IsPressed" Value="true">
<Setter Property="Background" TargetName="border" Value="{StaticResource Button.Pressed.Background}"/>
<Setter Property="BorderBrush" TargetName="border" Value="{StaticResource Button.Pressed.Border}"/>
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Background" TargetName="border" Value="{StaticResource Button.Disabled.Background}"/>
<Setter Property="BorderBrush" TargetName="border" Value="{StaticResource Button.Disabled.Border}"/>
<Setter Property="TextElement.Foreground" TargetName="contentPresenter" Value="{StaticResource Button.Disabled.Foreground}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
按钮:
<Button x:Name="ButtonPress" Style="{DynamicResource ButtonPressStyle}" Margin="133,0,133,15" Content="Press" Height="50" VerticalAlignment="Bottom" Click="ButtonPress_Click"/>
此外,如果在 C# 中也有任何方法可以做到这一点,请教我怎么做,
感谢任何帮助。
将此添加到您的触发器中属性
<Setter Property="RenderTransformOrigin" Value="0.5, 0.5"/>
<Setter Property="RenderTransform">
<Setter.Value>
<ScaleTransform ScaleX="1.2" ScaleY="1.2"/>
</Setter.Value>
</Setter>
最终代码
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Background" TargetName="border" Value="{StaticResource Button.MouseOver.Background}"/>
<Setter Property="BorderBrush" TargetName="border" Value="{StaticResource Button.MouseOver.Border}"/>
<Setter Property="Foreground" Value="Black"/>
<Setter Property="BorderThickness" Value="1.5"/>
<Setter Property="RenderTransformOrigin" Value="0.5, 0.5"/>
<Setter Property="RenderTransform">
<Setter.Value>
<ScaleTransform ScaleX="1.2" ScaleY="1.2"/>
</Setter.Value>
</Setter>
</Trigger>