创建自定义的“默认”设置Expression Blend 中的按钮 + WPF

发布于 2024-10-08 06:52:08 字数 689 浏览 3 评论 0原文

我已经完成了大约 95% 的任务,但仍然遇到一些问题。基本上我希望所有按钮控件的默认样式(使用 Expression Blend 将按钮拖动到表单时使用)的行为如下:

鼠标悬停时 - 将按钮字体的颜色更改为渐变并增加字体的大小比控件在“正常”状态下分配的字体大小大 2 磅。尺寸的增加很重要,因为“按钮”没有任何边框,它们只是带有阴影的纯文本。

单击时 - 再次更改字体颜色,删除阴影以使其显得凹陷(大小仍将保持“正常”字体大小 + 2 点)

我当前的方法给我带来了一些问题,并且代码有点太多在这里发帖,所以我希望从有更多经验的人那里得到一些关于替代方法的建议。

更喜欢不要有一个完全自定义的按钮控件(如“CustomButton”),而只是覆盖常规默认“Button”控件的外观/样式,因为它将是唯一的按钮类型该应用程序。但如果新的类/控件是唯一的方法,我会尝试一下。

前任。

<Button Content="Button1"/>
<Button Content="Button2" FontSize="18"/>
<Button Content="Button3" FontSize="24"/>

鼠标悬停时,字体大小应比每个按钮指定的大小大 +2。现在,我通过在按钮的真实文本(带有一些绑定)之上覆盖 TextBlock 来实现此目的,但它还不太有效。

I'm about 95% of the way to getting what I need but still have some issues. Basically I'd like the default style of all Button controls (to use when dragging a Button to the form using Expression Blend) to behave as below:

On mouse over - change the button font's coloring to a gradient AND increase the size of the font by 2 points more than the font size the control is assigned in it's "normal" state. The size increase is important as the "buttons" don't have any border, they're just plain text with a drop shadow.

On click - again change the font coloring, remove a drop shadow to make it appear depressed (the size would still remain the "normal" font size + 2 points)

My current method is giving me some issues and it's a bit too much code to post here, so I'm hoping to get a few suggestions for alternative approaches from someone with more experience at this.

I'd prefer not to have a totally custom Button control (like "CustomButton") and to simply override the regular default "Button" control's appearance/style since it'll be the only type of button in the application. But if a new class/control is the only way, I'll give that a try instead.

Ex.

<Button Content="Button1"/>
<Button Content="Button2" FontSize="18"/>
<Button Content="Button3" FontSize="24"/>

On mouseover, the fontsize should go to +2 larger than each button's assigned size. Right now I'm doing this by overlaying a TextBlock on top of the button's real text (with a few bindings), but it's not quite working yet.

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

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

发布评论

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

评论(1

凌乱心跳 2024-10-15 06:52:09

如果我理解正确,您在这里问两个问题:

  1. 如何更改 MouseOver 上按钮的外观
  2. 如何自动将其应用于所有按钮

关于 #1:不需要专门的自定义控件 - 在 WPF 中执行此操作的最简单方法是通过使用 Style(带有 IsMouseOver 的属性触发器)。

至于#2:通过使您的样式成为无键资源(不指定资源键),它将应用于与目标类型匹配的范围内的所有控件。

以下是您可以放置​​在窗口或应用程序资源中的样式示例:

<Style TargetType="{x:Type Button}">
        <Style.Triggers>
            <Trigger Property="IsMouseOver"
                     Value="True">
                <Setter Property="RenderTransform">
                    <Setter.Value>
                        <ScaleTransform ScaleX="1.05" ScaleY="1.05"/>
                    </Setter.Value>
                </Setter>
                <Setter Property="Foreground">
                    <Setter.Value>
                        <LinearGradientBrush>
                            <GradientStop Color="CornflowerBlue"
                                          Offset="0"/>
                            <GradientStop Color="DarkSlateBlue"
                                          Offset="1"/>
                        </LinearGradientBrush>
                    </Setter.Value>
                </Setter>
            </Trigger>
        </Style.Triggers>
        <Setter Property="RenderTransformOrigin"
                Value=".5,.5"/>
    </Style>

请注意,此样式不会将字体大小增加 2,而是将整个按钮缩放 5%。对于您的想法来说,这确实可能是更好(也更简单)的解决方案。如果您绝对需要增加相对字体大小(而不是分配绝对的新字体大小),这有点棘手,但也是可以做到的。

If I understand you correctly, you are asking two questions here:

  1. How to change the button's appearance on MouseOver
  2. How to automatically apply this to all buttons

Regarding #1: No need for a specialized custom control - the easiest way to do this in WPF is by using a Style (with a property trigger for IsMouseOver).

As for #2: By making your Style a keyless resource (not specifying a resource key), it will be applied to all controls in scope matching the target type.

Here's an example of a style you can place in your Window's or App's resources:

<Style TargetType="{x:Type Button}">
        <Style.Triggers>
            <Trigger Property="IsMouseOver"
                     Value="True">
                <Setter Property="RenderTransform">
                    <Setter.Value>
                        <ScaleTransform ScaleX="1.05" ScaleY="1.05"/>
                    </Setter.Value>
                </Setter>
                <Setter Property="Foreground">
                    <Setter.Value>
                        <LinearGradientBrush>
                            <GradientStop Color="CornflowerBlue"
                                          Offset="0"/>
                            <GradientStop Color="DarkSlateBlue"
                                          Offset="1"/>
                        </LinearGradientBrush>
                    </Setter.Value>
                </Setter>
            </Trigger>
        </Style.Triggers>
        <Setter Property="RenderTransformOrigin"
                Value=".5,.5"/>
    </Style>

Note that this style will not increase the font size by 2 - instead, it scales the entire button by 5%. This might indeed be the better (and easier) solution to what you have in mind. If you absolutely need to increase the relative font size (instead of assigning an absolute, new font size), it's a bit trickier but can be done too.

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