在App.xaml中,我有以下代碼:在代碼中設置WPF標籤的Style屬性?
<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>
這是爲了我的標籤提供一個通用模板。
在主XAML代碼,我有下面的代碼行:
<Label Content="Movies" Style="{StaticResource LabelTemplate}" Name="label1" />
不過,我想通過代碼來初始化樣式屬性。我曾嘗試:
label1.Style = new Style("{StaticResource LabelTemplate}");
和
label1.Style = "{StaticResource LabelTemplate}";
無論解決方案是有效的。
任何幫助將不勝感激:)。
是否有任何理由從UserControl的代碼隱藏中做到這一點?也許有更優雅的解決方案。 – csteinmueller