在xaml中创建图像样式

发布于 2024-09-16 18:13:32 字数 2584 浏览 2 评论 0原文

我不知道为什么我在做这件事时遇到这么多麻烦,这不应该很难,但我一定是混合不称职。有人能给我一个图像样式的 xaml 吗,其中图像的不透明度为 60%,鼠标悬停时淡入到 100,鼠标移开时回到 60%,单击时发光 0.2 秒。

或者只是告诉我如何混合?

谢谢

解决方案很简单:

<Style x:Key="FadeImageButton" TargetType="Button">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <Grid x:Name="grid" Width="16" Height="16">
                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup x:Name="CommonStates">
                            <VisualStateGroup.Transitions>
                                <VisualTransition GeneratedDuration="0:0:0.2"/>
                                <VisualTransition GeneratedDuration="0:0:0.2" To="Normal"/>
                                <VisualTransition GeneratedDuration="0:0:0.2" To="MouseOver"/>
                            </VisualStateGroup.Transitions>
                            <VisualState x:Name="Normal">
                                <Storyboard>
                                    <DoubleAnimation Duration="0" To="0.6" Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="grid" d:IsOptimized="True"/>
                                </Storyboard>
                            </VisualState>
                            <VisualState x:Name="MouseOver">
                                <Storyboard>
                                    <DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="grid" d:IsOptimized="True"/>
                                </Storyboard>
                            </VisualState>
                            <VisualState x:Name="Pressed"/>
                            <VisualState x:Name="Disabled"/>
                        </VisualStateGroup>
                        <VisualStateGroup x:Name="FocusStates">
                            <VisualState x:Name="Focused"/>
                            <VisualState x:Name="Unfocused"/>
                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>
                    <ContentPresenter x:Name="contentPresenter" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}"/>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

I don't know why I'm having so much trouble doing this, it shouldn't be hard, but I must be Blend-incompetent. Can someone give me the xaml for an image style where the image is at 60% opacity, on mouseover fades in to 100, mouseout back to 60% and onclick glows for a 0,2 sec.

Or just tell me how to do in blend?

thank you

Solution turned out to be simple enough:

<Style x:Key="FadeImageButton" TargetType="Button">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <Grid x:Name="grid" Width="16" Height="16">
                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup x:Name="CommonStates">
                            <VisualStateGroup.Transitions>
                                <VisualTransition GeneratedDuration="0:0:0.2"/>
                                <VisualTransition GeneratedDuration="0:0:0.2" To="Normal"/>
                                <VisualTransition GeneratedDuration="0:0:0.2" To="MouseOver"/>
                            </VisualStateGroup.Transitions>
                            <VisualState x:Name="Normal">
                                <Storyboard>
                                    <DoubleAnimation Duration="0" To="0.6" Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="grid" d:IsOptimized="True"/>
                                </Storyboard>
                            </VisualState>
                            <VisualState x:Name="MouseOver">
                                <Storyboard>
                                    <DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="grid" d:IsOptimized="True"/>
                                </Storyboard>
                            </VisualState>
                            <VisualState x:Name="Pressed"/>
                            <VisualState x:Name="Disabled"/>
                        </VisualStateGroup>
                        <VisualStateGroup x:Name="FocusStates">
                            <VisualState x:Name="Focused"/>
                            <VisualState x:Name="Unfocused"/>
                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>
                    <ContentPresenter x:Name="contentPresenter" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}"/>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如果没结果 2024-09-23 18:13:32

你当然不是无能。图像没有状态,因此样式并不是答案。

您可以为图像创建的唯一样式是针对一种固定状态的,因此您可以添加 60% 的不透明度,但仅此而已。

您的选项是:

  • 创建 EnterImage 和 LeaveImage
    使用 ControlStoryboardAction 行为(在 MouseEnter 和 MouseLeave 事件上)播放的故事板。
  • 创建自定义行为并将其附加到图像。
  • 将图像放置在另一个具有状态的控件中(可能是按钮)
  • 将图像放置在具有图像属性的用户控件中
  • 创建自定义控件

最简单的是选项 1,但它需要将多个属性附加到每个图像,因此需要进行更多的拖动和单击操作创作它们。

如果您让我们知道您喜欢哪个选项,我也许可以发布一个示例。

You are certainly not incompetent. Images do not have states, so a style is not the answer.

The only styles you can create for images are for one fixed state, so you could add the 60% opacity, but not much else.

Your options are:

  • Create EnterImage and LeaveImage
    storyboards that are played with ControlStoryboardAction behaviours (on MouseEnter and MouseLeave events).
  • Create a custom behaviour and attach that to the images.
  • Place the image in another control that has states (maybe a button)
  • Place the image in a user control with an image property
  • Create a custom control

The simplest is option 1, but it requires attaching several properties to each image so more drags and clicks to author them.

If you let us know which option you prefer I may be able to post an example.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文