gpt4 book ai didi

c# - 使用模板更改悬停/单击时的按钮背景图像

转载 作者:行者123 更新时间:2023-12-01 20:15:35 24 4
gpt4 key购买 nike

我有一个带有大量按钮的应用程序,我想通过悬停/单击效果使其更加奇特。我希望有人可以为此使用样式模板,这样我就不必为每个按钮编写触发器,但我有点陷入了这个阶段。

我想您已经大致了解了我想通过以下代码片段实现的目标:

<Button BorderBrush="#00000000" Foreground="#00000000" Height="20" HorizontalContentAlignment="Right" IsEnabled="True" Name="btnMinimizeWindow" Width="21" DockPanel.Dock="Right" Margin="0,0,4,2" BorderThickness="0" Focusable="False" Style="{StaticResource ModernButton}">
<Button.Background>
<ImageBrush ImageSource="/MyApp;component/Images/btnMinimize.png" />
</Button.Background>
<Button.Resources>
<DataTemplate x:Key="Default" >
<Image Source="/MyApp;component/Images/btnMinimize.png" />
</DataTemplate>
<DataTemplate x:Key="Hover">
<Image Source="/MyApp;component/Images/btnMinimizeHover.png" />
</DataTemplate>
<DataTemplate x:Key="Active">
<Image Source="/MyApp;component/Images/btnMinimizeActive.png" />
</DataTemplate>
</Button.Resources>
</Button>

和模板文件:

<Style x:Key="ModernButton" TargetType="{x:Type Button}">
<Setter Property="Padding" Value="1"/>
<Setter Property="Background" Value="Transparent" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border Name="border" Background="{TemplateBinding Background}">
<ContentPresenter Name="Content" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
Margin="{TemplateBinding Padding}"
RecognizesAccessKey="True"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">

</Trigger>
</Trigger>
<Trigger Property="IsPressed" Value="True">

</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

我该如何解决这个问题?是否有可能以这种方式做到这一点,或者我是否必须用数百万个触发器使我的代码变得困惑?

最佳答案

您可以定义attached properties用于状态 NormalMouseOverPressed(也许还有更多)的背景图像。您可以将这些附加属性用于控件模板中单独图像控件的 Source 属性,并修改例如VisualState 时图像的不透明度变化。

示例样式:

<Style TargetType="Button">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Grid>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal"/>
<VisualState x:Name="Disabled"/>
<VisualState x:Name="MouseOver">
<Storyboard>
<DoubleAnimation Storyboard.TargetName="mouseOverBackgroundImage" Storyboard.TargetProperty="Opacity" Duration="0:0:0.1" To="1"/>
</Storyboard>
</VisualState>
<VisualState x:Name="Pressed">
<Storyboard>
<DoubleAnimation Storyboard.TargetName="pressedBackgroundImage" Storyboard.TargetProperty="Opacity" Duration="0:0:0.1" To="1"/>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Image Name="normalBackgroundImage" Source="{TemplateBinding local:BackgroundImages.NormalBackgroundImage}"/>
<Image Name="mouseOverBackgroundImage" Source="{TemplateBinding local:BackgroundImages.MouseOverBackgroundImage}" Opacity="0"/>
<Image Name="pressedBackgroundImage" Source="{TemplateBinding local:BackgroundImages.PressedBackgroundImage}" Opacity="0"/>
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

在带有附加属性集的按钮中使用:

<Button local:BackgroundImages.NormalBackgroundImage="C:\Users\Public\Pictures\Sample Pictures\Tulips.jpg"
local:BackgroundImages.MouseOverBackgroundImage="C:\Users\Public\Pictures\Sample Pictures\Desert.jpg"
local:BackgroundImages.PressedBackgroundImage="C:\Users\Public\Pictures\Sample Pictures\Penguins.jpg"
Content="Hello"/>

最后是这些附加属性的定义:

public static class BackgroundImages
{
public static readonly DependencyProperty NormalBackgroundImageProperty =
DependencyProperty.RegisterAttached("NormalBackgroundImage", typeof(ImageSource), typeof(BackgroundImages));

public static readonly DependencyProperty MouseOverBackgroundImageProperty =
DependencyProperty.RegisterAttached("MouseOverBackgroundImage", typeof(ImageSource), typeof(BackgroundImages));

public static readonly DependencyProperty PressedBackgroundImageProperty =
DependencyProperty.RegisterAttached("PressedBackgroundImage", typeof(ImageSource), typeof(BackgroundImages));

public static ImageSource GetNormalBackgroundImage(DependencyObject obj)
{
return (ImageSource)obj.GetValue(NormalBackgroundImageProperty);
}

public static void SetNormalBackgroundImage(DependencyObject obj, ImageSource value)
{
obj.SetValue(NormalBackgroundImageProperty, value);
}

public static ImageSource GetMouseOverBackgroundImage(DependencyObject obj)
{
return (ImageSource)obj.GetValue(MouseOverBackgroundImageProperty);
}

public static void SetMouseOverBackgroundImage(DependencyObject obj, ImageSource value)
{
obj.SetValue(MouseOverBackgroundImageProperty, value);
}

public static ImageSource GetPressedBackgroundImage(DependencyObject obj)
{
return (ImageSource)obj.GetValue(PressedBackgroundImageProperty);
}

public static void SetPressedBackgroundImage(DependencyObject obj, ImageSource value)
{
obj.SetValue(PressedBackgroundImageProperty, value);
}
}

关于c# - 使用模板更改悬停/单击时的按钮背景图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9913422/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com