在Silverlight 4 Beta中,将FontFamily应用于所有控件的方法如下:
public static class FontFamily
{
public const string Arial = "Arial";
public const string TimesNewRoman = "Times New Roman";
public const string Verdana = "Verdana";
}
<ResourceDictionary>
<Style TargetType="TextBlock">
<Setter Property="FontFamily" Value="FontFamily.Arial" />
</Style>
<Style TargetType="TextBox">
<Setter Property="FontFamily" Value="FontFamily.Verdana" />
</Style>
<Style TargetType="Button">
<Setter Property="FontFamily" Value="FontFamily.TimesNewRoman" />
</Style>
</ResourceDictionary>
</Application.Resources>
public MainWindow()
{
InitializeComponent();
foreach (var control in this.GetVisualDescendants().OfType<Control>())
{
control.FontFamily = new FontFamily(FontFamily.Arial);
}
}
private IEnumerable<DependencyObject> GetVisualDescendants(DependencyObject root = null)
{
if (root == null)
{
root = this;
}
int count = VisualTreeHelper.GetChildrenCount(root);
for (int i = 0; i< count; i++)
{
var child = VisualTreeHelper.GetChild(root, i);
yield return child;
foreach (var descendant in GetVisualDescendants(child))
{
yield return descendant;
}
}
}
这样,您就可以将FontFamily应用于Silverlight 4 Beta中的所有控件了。请注意,这只是一个示例,您可以根据自己的需求进行调整。
领取专属 10元无门槛券
手把手带您无忧上云