在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)
两种解决方案都无效.
任何帮助,将不胜感激 :).