![](/img/trans.png)
[英]How to have a generic.xaml for Silverlight 3 and Silverlight 4?
[英]Silverlight generic.xaml
我可以在 Silverlight 中使用 generic.xaml 來設置應用程序中所有 TextBlock 的樣式嗎?
<ResourceDictionary xmlns="http://schemas.microsoft.com/client/2007"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style TargetType="TextBlock">
<Setter Property="Foreground"
Value="White" />
<Setter Property="FontSize"
Value="24" />
</Style>
</ResourceDictionary>
我期待這能工作,但它沒有:-(
在整個應用程序中應用 styles 的最簡單方法是什么?
謝謝,馬克
編輯
感謝您的答復。 我沒有收到錯誤,只是沒有應用 styles。 我在 Internet 上找到的所有示例(包括您列出的示例)都是用於自定義控件的樣式。 我可以這樣做,但我只想設置默認控件的樣式:
<TextBlock Text="Style me!!" Grid.Row="2" />
我是否需要從 page.xaml 添加對 generic.xaml 的引用? 我是否需要命名並引用 generic.xaml 樣式作為資源?
再次感謝,馬克
在 Silverlight 4 中,您可以使用隱式樣式!
通過設置 TargetType 並省略 ResourceKey 來通用/自動設置 Styles 僅適用於 WPF,不適用於 Silverlight。
如果您想在整個應用程序中使用 Style,您可以將其添加到 App.xaml 中的 Resources 集合中,但要使用它,它需要 ax:Key 並且您必須將其作為 StaticResource 綁定單獨應用到每個實例您想要使用它的目標類型。
請注意,您還可以使用 Silverlight 工具包中的 ImplicitStyleManager 來執行此操作:
將樣式應用於層次結構中的頂級控件,它將向下傳播到所有子控件。
我確實相信這是在 Silverlight 3 中。但是其他人是對的,您需要在 Silverlight 2 中手動設置每個樣式。
嘗試類似的東西。
<UserControl.Resources>
<Style TargetType="TextBlock" x:Name="tbStyle">
<Setter Property="Foreground" Value="White" />
<Setter Property="FontSize" Value="24" />
</Style>
</UserControl.Resources>
然后在使用您的 TextBlock 時。
<TextBlock Style="{StaticResource tbStyle}" />
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.