Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用C在运行时创建自定义的数据透视项标题#_C#_Xaml_Windows Phone 7_Pivotitem - Fatal编程技术网

C# 使用C在运行时创建自定义的数据透视项标题#

C# 使用C在运行时创建自定义的数据透视项标题#,c#,xaml,windows-phone-7,pivotitem,C#,Xaml,Windows Phone 7,Pivotitem,我正在尝试在加载时将数据透视项动态添加到数据透视中。我需要一些屏幕属性,标准的pivot项目标题字体对我来说太大了。一些论坛搜索导致了此解决方案: <controls:PivotItem> <controls:PivotItem.Header> <TextBlock FontSize="{StaticResource PhoneFontSizeLarge} Text="MyHeaderText"/> </controls:Pivo

我正在尝试在加载时将数据透视项动态添加到数据透视中。我需要一些屏幕属性,标准的pivot项目标题字体对我来说太大了。一些论坛搜索导致了此解决方案:

<controls:PivotItem>
   <controls:PivotItem.Header>
      <TextBlock FontSize="{StaticResource PhoneFontSizeLarge} Text="MyHeaderText"/>
   </controls:PivotItem.Header>
</controls:PivotItem>


只需创建一个
Pivot
对象和一些
PivotItems
对象,然后将这些
PivotItems
添加到
Pivot
。最后,将这个
Pivot
添加到您的
LayoutRoot
,它很可能是一个
网格

像这样的,

    void PivotPage2_Loaded(object sender, RoutedEventArgs e)
    {
        var pivot = new Pivot();
        var textBlock = new TextBlock { Text = "header 1", FontSize = 32 };
        var pivotItem1 = new PivotItem { Header = textBlock };

        var textBlock2 = new TextBlock { Text = "header 2", FontSize = 32 };
        var pivotItem2 = new PivotItem { Header = textBlock2 };

        pivot.Items.Add(pivotItem1);
        pivot.Items.Add(pivotItem2);

        this.LayoutRoot.Children.Add(pivot);
    }

以下代码将
Pivot
中所有现有
Pivot元素的
FontSize
设置为给定值。它还(粗略地)调整收割台区域的高度

代码深入到
Pivot
的子项中,并搜索要修改的正确项:
PivotHeaderItem
(单个标题)和
PivotHeadersControl
(包含所有标题)

使用Microsoft.Phone.Controls.Primitives;
委托void ChildProc(DependencyObject o);
//将委托过程应用于给定类型的所有子级
私有void doforchildren递归(DependencyObject o,类型typeFilter,ChildProc proc)
{
//检查我们是否有合适类型的孩子
if(o.GetType()==typeFilter)
{
proc(o);
}
//递归:深入到子层次结构中一个层次
for(int i=0;i{lastFontSize=((PivotHeaderItem)o).FontSize;((PivotHeaderItem)o.FontSize=FontSize;});
//根据字体大小的变化调整页眉控制高度
doForChildren递归(pivot,typeof(PivotHeadersControl),(o)=>{((PivotHeadersControl)o.Height-=(lastFontSize-fontSize)*1.33;});
}
私有无效按钮1\u单击(对象发送者,路由目标)
{
//使标题项的FontSize==PhoneFontSizeLarge
调整PivotHeaderFontSize(pivot,(double)资源[“PhoneFontSizeLarge”]);
}

如果你想知道页眉高度计算中的
*1.33
是从哪里来的,它的灵感来自于。

@Heinrich I jus想知道,当页面加载时,你是否经历过某种跳跃。我已经在xaml文件中的上述方法中创建了pivot头。我使用这种方法是因为我想使用自定义字体。当我运行应用程序时,标题会在数据透视项之后加载,因此UI会被向下推。??有什么办法吗?我没办法。当我尝试将pivotItem添加到pivot.Items时,我得到一个ArgumentException“参数不正确”。如果我将textBlock部分更改为普通文本{Header=“Header 1”},它会起作用。实际上可以插入第一个数据透视项,但添加第二个数据透视项时,它会失败。
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"]);
}