C# 在代码中设置 WPF 标签的 Style 属性?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10686917/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 14:49:25  来源:igfitidea点击:

Setting the Style property of a WPF Label in code?

c#wpfuser-interfacelabel

提问by Daniel

In App.xaml, I have the following code:

在 App.xaml 中,我有以下代码:

<Application.Resources>
    <Style x:Key="LabelTemplate" TargetType="{x:Type Label}">
        <Setter Property="Height" Value="53" />
        <Setter Property="Width" Value="130" />
        <Setter Property="HorizontalAlignment" Value="Left" />
        <Setter Property="Margin" Value="99,71,0,0" />
        <Setter Property="VerticalAlignment" Value= "Top" />
        <Setter Property="Foreground" Value="#FFE75959" />
        <Setter Property="FontFamily" Value="Calibri" />
        <Setter Property="FontSize" Value="40" />
    </Style>
</Application.Resources>

This is meant to provide a generic template for my labels.

这是为了为我的标签提供通用模板。

In the main XAML code, I have the following line of code:

在主要的 XAML 代码中,我有以下代码行:

<Label Content="Movies" Style="{StaticResource LabelTemplate}" Name="label1" />

However, I'd like to initialize the Style property through code. I have tried:

但是,我想通过代码初始化 Style 属性。我试过了:

label1.Style = new Style("{StaticResource LabelTemplate}");

and

label1.Style = "{StaticResource LabelTemplate}";

Neither solution was valid.

两种解决方案都无效。

Any help would be appreciated :).

任何帮助,将不胜感激 :)。

采纳答案by Damascus

Where in code are you trying to get the style? Code behind?

您试图在代码中的何处获得样式?后面的代码?

You should write this:

你应该这样写:

If you're in code-behind:

如果您处于代码隐藏状态:

Style style = this.FindResource("LabelTemplate") as Style;
label1.Style = style;

If you're somewhere else

如果你在别处

Style style = Application.Current.FindResource("LabelTemplate") as Style;
label1.Style = style;

Bottom note:don't name a Stylewith the keyword Template, you'll eventually end up confusing a Styleand a Template, and you shouldn't as those are two different concepts.

底注:不要Style用关键字命名 a Template,你最终会混淆 aStyle和 a Template,你不应该因为它们是两个不同的概念。

回答by Allen

Please check for null style result or you will be sad... ... if (style != null) this.Style = style;

请检查空样式结果,否则您会感到难过... ... if (style != null) this.Style = style;

回答by Juan Pablo Gomez

Maybe an old question, but if you are trying W10 UWP app must use resources collection of each object or resources collection of Application object

也许是一个老问题,但如果你正在尝试 W10 UWP 应用程序必须使用每个对象的资源集合或应用程序对象的资源集合

KeyValuePair<object,object> styl = this.Resources
    .Where(x => x.Key.ToString() == "MyStyleTemplateName")
    .FirstOrDefault();
if (styl.Value != null)
    Style MyStyle = (Style)styl.Value;

Where MyStyleTemplateNamemust be defined as a resource of this

其中MyStyleTemplateName必须定义为this的资源