获取按钮样式名称
我在 Window.Resources 上有多种样式,我用 C# 将它们应用于多个按钮。然后我需要更改样式,但首先我需要知道应用于我想要更改样式的按钮的当前样式是什么。 我找不到从按钮获取样式名称的方法!
I have several styles on Window.Resources that I apply to several buttons with C#. Then I need to change the style but first I need to know what is the current style that is applied to the button I want to change the style.
I can't find the way to get the style name from the button!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您是否尝试过
Button.Style
属性?如果使用资源Key
完成样式的显式设置,那么您应该使用Button.Style
属性获取按钮的当前样式,否则收集所有会有点棘手控制级别的>样式
相关信息。这是有原因的。样式是继承的,并且可以在不同的元素范围(例如 App、Window、UserControl、Ancestor UIElements 以及最后的单个控件)进行覆盖。因此,当您访问
Button.Style
属性时,您将获得一个样式,该样式是应用于Button
的最后一个直接样式。如果样式是BasedOn
另一个Style
,则Style.BasedOn
将为您提供父/基Style
。同样,如果该BasedOn
样式是从另一个Style
派生的,我们将得到Style.basedOn.BasedOn
... 等等。我希望这使得感觉。 :-)
Did you try
Button.Style
property? If explcit setting of style is done using resourceKey
then you should get the current style of the button usingButton.Style
propetry otherwise it is a little tricky to gather allStyle
related information at a control level.And there are reasons for this. Styles are inherited and could be overriden at distinct element scopes such as App, Window, UserControl, Ancestor UIElements and finally the individual control. So when you access
Button.Style
property you get a style that was the last immediate style applied to theButton
. If the style isBasedOn
anotherStyle
thenStyle.BasedOn
will give you the parent / baseStyle
. Again if thatBasedOn
style is derived from anotherStyle
we will get that asStyle.basedOn.BasedOn
... etc.I hope this makes sense. :-)
我认为如果您以这种方式处理问题,那么您在设计/架构方面就犯了错误。如果您需要有条件地更改样式,您可以使用 数据绑定和模板。
I think you are making a mistake in terms of design/architecture if you approach your issue this way. If you need to change styles conditionally you can create UI-elements based on objects which hold the relevant information using data-binding and templating.
这是一个好问题(+1)。
这只是我的想法,可能不太准确。我怀疑为 UI 控件获取样式是否有意义。假设您将样式“style1”应用于 UI 控件,然后您可以设置单独的属性,例如前景/背景......现在,样式是什么?
如果您想维护/跟踪按钮的状态,则应该将其作为视觉状态或在后面的代码(ViewModel/Model)中处理。
That's a good question (+1).
This is just my thought which may not be very accurate. I doubt if it makes sense to get a style for a UI control. Suppose you apply style "style1" to an UI control and then you can set individual attributes like foreground/background.... Now, what would be the style?
If you want to maintain/track the state of the button, that should be handled either as visual states or in your code behind (ViewModel/Model) probably.
请参阅:样式
我想这就是你的正在寻找?
See: Style
I think that's what you're looking for?
感谢您的回答,我正在使用另一个 stackoverflow 中的这个函数..它可以工作并将样式名称返回到字符串中!
Thanks for your answers, I'm using this function from another stackoverflow.. it works and returns the style name into a string!
@最大限度,
我是 WPF 新手,必须在它可以具有的两种已知样式之一之间切换 Border 对象的样式。我没有在 FindNameFromResource 中使用线性搜索,而是这样做了......
@Max,
I'm new to WPF, and had to toggle my Border object's style between one of two known Styles it can have. Rather than use the linear search in FindNameFromResource, I instead instead did this ...