相关疑难解决方法(0)

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

在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>
Run Code Online (Sandbox Code Playgroud)

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

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

<Label Content="Movies" Style="{StaticResource LabelTemplate}" Name="label1" />
Run Code Online (Sandbox Code Playgroud)

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

label1.Style = new Style("{StaticResource LabelTemplate}");
Run Code Online (Sandbox Code Playgroud)

label1.Style = "{StaticResource LabelTemplate}";
Run Code Online (Sandbox Code Playgroud)

两种解决方案都无效.

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

c# wpf user-interface label

72
推荐指数
1
解决办法
9万
查看次数

标签 统计

c# ×1

label ×1

user-interface ×1

wpf ×1