获取按钮样式名称

发布于 2024-12-05 05:06:23 字数 109 浏览 5 评论 0原文

我在 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 技术交流群。

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

发布评论

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

评论(6

時窥 2024-12-12 05:06:23

您是否尝试过 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 resource Key then you should get the current style of the button using Button.Style propetry otherwise it is a little tricky to gather all Style 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 the Button. If the style is BasedOn another Style then Style.BasedOn will give you the parent / base Style. Again if that BasedOn style is derived from another Style we will get that as Style.basedOn.BasedOn... etc.

I hope this makes sense. :-)

可爱咩 2024-12-12 05:06:23

我认为如果您以这种方式处理问题,那么您在设计/架构方面就犯了错误。如果您需要有条件地更改样式,您可以使用 数据绑定模板

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.

夏至、离别 2024-12-12 05:06:23

这是一个好问题(+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.

别靠近我心 2024-12-12 05:06:23

请参阅:样式

public void FooFunc()
{
    Button myButton = ...;

    Console.WriteLine("The Style: {0}", myButton.Style);
}

我想这就是你的正在寻找?

See: Style

public void FooFunc()
{
    Button myButton = ...;

    Console.WriteLine("The Style: {0}", myButton.Style);
}

I think that's what you're looking for?

雄赳赳气昂昂 2024-12-12 05:06:23

感谢您的回答,我正在使用另一个 stackoverflow 中的这个函数..它可以工作并将样式名称返回到字符串中!

static public string FindNameFromResource(ResourceDictionary dictionary, object resourceItem)

static public string FindNameFromResource(ResourceDictionary dictionary, object resourceItem)
        {
            foreach (object key in dictionary.Keys)
            {
                if (dictionary[key] == resourceItem)
                {
                    return key.ToString();
                }
            }

            return null;
        }

Thanks for your answers, I'm using this function from another stackoverflow.. it works and returns the style name into a string!

static public string FindNameFromResource(ResourceDictionary dictionary, object resourceItem)

static public string FindNameFromResource(ResourceDictionary dictionary, object resourceItem)
        {
            foreach (object key in dictionary.Keys)
            {
                if (dictionary[key] == resourceItem)
                {
                    return key.ToString();
                }
            }

            return null;
        }
滥情哥ㄟ 2024-12-12 05:06:23

@最大限度,
我是 WPF 新手,必须在它可以具有的两种已知样式之一之间切换 Border 对象的样式。我没有在 FindNameFromResource 中使用线性搜索,而是这样做了......

Style normal = (Style)this.Resources["NormalBorder"];
Style strong = (Style)this.Resources["StrongBorder"];
border.Style = border.Style == normal ? strong : normal;

@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 ...

Style normal = (Style)this.Resources["NormalBorder"];
Style strong = (Style)this.Resources["StrongBorder"];
border.Style = border.Style == normal ? strong : normal;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文