次のコードは、 a内のFontSize
既存のすべてのを指定された値に設定します。また、ヘッダー領域の高さを (大まかに) 調整します。PivotElement
Pivot
コードはの子に飛び込み、変更する適切な項目(単一のヘッダー) および(すべてのヘッダーを含む)Pivot
を検索します。PivotHeaderItem
PivotHeadersControl
using Microsoft.Phone.Controls.Primitives;
delegate void ChildProc(DependencyObject o);
// applies the delegate proc to all children of a given type
private void DoForChildrenRecursively(DependencyObject o, Type typeFilter, ChildProc proc)
{
// check that we got a child of right type
if (o.GetType() == typeFilter)
{
proc(o);
}
// recursion: dive one level deeper into the child hierarchy
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(o); i++)
{
DoForChildrenRecursively(VisualTreeHelper.GetChild(o, i), typeFilter, proc);
}
}
// applies the given font size to the pivot's header items and adjusts the height of the header area
private void AdjustPivotHeaderFontSize(Pivot pivot, double fontSize)
{
double lastFontSize = fontSize;
DoForChildrenRecursively(pivot, typeof(PivotHeaderItem), (o) => { lastFontSize = ((PivotHeaderItem)o).FontSize; ((PivotHeaderItem)o).FontSize = fontSize; });
// adjust the header control height according to font size change
DoForChildrenRecursively(pivot, typeof(PivotHeadersControl), (o) => { ((PivotHeadersControl)o).Height -= (lastFontSize - fontSize) * 1.33; });
}
private void button1_Click(object sender, RoutedEventArgs e)
{
// make header items having FontSize == PhoneFontSizeLarge
AdjustPivotHeaderFontSize(pivot, (double)Resources["PhoneFontSizeLarge"]);
}
ヘッダーの高さの計算がどこから来たのか疑問に思っている場合は、このブログ投稿*1.33
に触発されました。