可以使用自定义属性重新设计按钮控件吗?

发布于 2024-08-19 13:02:11 字数 804 浏览 5 评论 0 原文

我为自己制作了一个 TransparentButton 样式,使 Button 部分按照我想要的方式运行(鼠标悬停、启用等),但我无法做到是在样式中正确设置内容。目前,我为每个按钮手动设置 中的所有内容,显然这些内容需要进入 Style 中。我已将样式的 ContentTemplate 设置为仅包含 ImageLabelStackPanel。问题是,我不知道如何在

所以,总而言之,我只想要一个一致的按钮样式,其中按钮内容只是 ImageLabelStackPanel,而且我希望能够在我的 GUI 中使用简单的东西创建它,例如:

<Button Style={DynamicResource TransparentButton}"
        Label="Click Me" Image="Images/MyImage.png" />

任何提示将不胜感激!我希望我在这里走在正确的轨道上......

I made myself a TransparentButton style that makes the Button portion behave the way I want it to (mouseover, enabled, etc), but what I haven't been able to do is set the content correctly in the style. Currently, I manually set everything in <Button.Content> for every button, and clearly that stuff needs to go into the Style. I have set the ContentTemplate for the style to a StackPanel that just contains an Image and a Label. The problem is, I don't know how to specify in my <Button ...> markup the Label's text and the Image's Source. I figured that it had to do with TemplateBinding somehow, but I've been searching like crazy and can't seem to find the information.

So, in summary, I just want a consistent button style where the button content is just a StackPanel of an Image and a Label, and I want to be able to create it in my GUI with something simple like:

<Button Style={DynamicResource TransparentButton}"
        Label="Click Me" Image="Images/MyImage.png" />

Any tips would be much appreciated! I hope I'm on the right track here...

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

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

发布评论

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

评论(1

陪你到最终 2024-08-26 13:02:11

为了创建这样的自定义属性,您需要创建一个 CustomControl 而不仅仅是使用 Button 样式。

不过,这相当简单 - 只需创建一个自定义控件来子类化按钮,并添加新属性(作为依赖属性)。这将使它变得可设计,而且还使您能够使用您所描述的语法强制这些属性始终可用(除了将 更改为 )。

有关详细信息,请参阅 MSDN 上的控件创作概述

In order to create custom properties like this, you'll need to make a CustomControl instead of just using a Button style.

This is fairly easy, though - just make a custom control that subclasses button, and adds your new properties (as dependency properties). This will make it stylable, but also provide you the ability to enforce that those properties are always available, with the syntax you're describing (other than changing <Button to <local:MyButton).

For details, see the Control Authoring Overview on MSDN.

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