PivotItems在绑定后不会调整大小

时间:2014-10-29 08:45:08

标签: c# xaml mvvm windows-phone pivot

我绑定了一个PivotItems,但根据我的保证金声明,某些项目没有正确调整大小。那是为什么?

编辑:这是我的新代码,但仍有差距..

    <Pivot x:Name="TpsSegmentsPivot" Title="Locator" Foreground="#FF888888" Style="{StaticResource PivotStyle1}" SelectionChanged="Pivot_SelectionChanged" Margin="0" Grid.Row="1" ItemTemplate="{StaticResource TpTemplate}" ItemsSource="{Binding DataSource}">
        <Pivot.HeaderTemplate>
            <DataTemplate>
                <Grid>
                    <TextBlock Text="{Binding id}" Margin="0, 16, 0, 0" Foreground="#FF888888" FontSize="32" FontFamily="Segoe WP" FontWeight="Light"/>
                </Grid>
            </DataTemplate>
        </Pivot.HeaderTemplate>
    </Pivot>

这是模板:

<DataTemplate x:Key="TpTemplate">
    <ListBox Background="Black"
             ItemsSource="{Binding Seg}"
             ItemTemplate="{StaticResource SectionUCTemplate}"
             HorizontalAlignment="Stretch"
             VerticalAlignment="Top">
        <ListBox.ItemContainerStyle>
            <Style TargetType="ListBoxItem">
                <Setter Property="HorizontalContentAlignment" Value="Stretch"></Setter>
            </Style>
        </ListBox.ItemContainerStyle>
    </ListBox>
</DataTemplate>

以下是红色pivotItem的屏幕截图..

enter image description here

2 个答案:

答案 0 :(得分:1)

我认为额外空间的问题是你在DataTemplate标签内使用的Piviot Item。删除它并将标头绑定到标头模板。我以前在Pivots上使用过数据绑定,从未见过这个问题。

来自我发布的一个应用:

<controls:Pivot Title="CAMPING CHECKLIST" ItemsSource="{Binding FilteredCategories}" Name="MainPivot">
    <controls:Pivot.HeaderTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Name}" />
        </DataTemplate>
    </controls:Pivot.HeaderTemplate>
    <controls:Pivot.ItemTemplate>
        <DataTemplate>
            <!-- Your pivot item content here -->
        </DataTemplate>
    </controls:Pivot.ItemTemplate>
</controls:Pivot>

我相信PivotItem中的PivotItem是您问题的根源。

您的代码应类似于:

<controls:Pivot ItemsSource="{Binding tripTypeViewModel.TripTypeViewModelDataSource}" x:Name="TripsSegmentsPivot" Title=" " Foreground="#FF888888" Style="{StaticResource PivotStyle1}" SelectionChanged="Pivot_SelectionChanged" Grid.Row="1">
    <controls:Pivot.HeaderTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding DataContext.tripTypeViewModel.HeaderText, ElementName=TripsSegmentsPivot}" Margin="0, 16, 0, 0" Foreground="#FF888888" FontSize="32" FontFamily="Segoe WP" FontWeight="Light"/>
        </DataTemplate>
    </controls:Pivot.HeaderTemplate>
    <controls:Pivot.ItemTemplate>
        <DataTemplate>
                <ListBox x:Name="ItemsLB" ItemsSource="{Binding DataContext.tripTypeViewModel.Segment, ElementName=TripsSegmentsPivot}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="Transparent">
                    <ListBox.ItemContainerStyle>
                        <Style TargetType="ListBoxItem">
                            <Setter Property="HorizontalContentAlignment" Value="Stretch"></Setter>
                        </Style>
                    </ListBox.ItemContainerStyle>
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <local:SectionUC HorizontalAlignment="Stretch" Grid.Row="1" VerticalAlignment="Top"/>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>
        </DataTemplate>
    </controls:Pivot.ItemTemplate>
</controls:Pivot>

第一张图片是我描述它的方式,第二张图片是你现在的描述:

mine yours

答案 1 :(得分:-1)

这是PivotItems的标准行为,因为它们始终以边距显示。你应该设置一个负余量:

margin="-20,0,-20,0"