如果ItemsControl放在windows phone 8中的pivot控件的DataTemplate中,则更改ItemTemplate

时间:2013-09-17 05:33:24

标签: wpf xaml windows-phone-7 windows-phone-8 itemtemplateselector

我是Windows Phone 8开发的新手。我正在尝试在我的应用程序中创建类似布局的日历。为此,我在下面的xaml中使用了Pivot控件

<phone:Pivot x:Name="Piv" ItemsSource="{Binding Months}" Grid.Row="1" Margin="0,-10,0,0">
    <phone:Pivot.HeaderTemplate>
        <DataTemplate>
            <TextBlock Margin="0,0,0,0" Text="{Binding Name}" Height="70" FontSize="50"/>
        </DataTemplate>
    </phone:Pivot.HeaderTemplate>
    <phone:Pivot.ItemTemplate>
        <DataTemplate>
            <StackPanel Margin="0,-20,0,0">
                <Grid Margin="0,0,0,0" >        
                    <!-- Header items goes here -->
                </Grid>
                <ItemsControl x:Name="CalendarControl"  ItemsSource="{Binding Days}" ItemTemplate="{StaticResource HorizontalPivotTemplate}" >
                    <ItemsControl.ItemsPanel>
                        <ItemsPanelTemplate>
                            <Grid x:Name="CalGrid">
                                <!--Colums and rows for layout goes here-->
                            </Grid>
                        </ItemsPanelTemplate>
                    </ItemsControl.ItemsPanel>
                </ItemsControl>
            </StackPanel>
        </DataTemplate>
    </phone:Pivot.ItemTemplate>
</phone:Pivot>

对于Itemcontrol,我将两个DataTemplates定义为页面资源,如下所示。

<phone:PhoneApplicationPage.Resources>
        <DataTemplate x:Key="VerticalPivotTemplate">
            <Grid  MinWidth="65" local:ItemsGridLayout.GridRow="{Binding Week}" 
                                        local:ItemsGridLayout.GridColumn="{Binding WeekDay}">
                <Border BorderThickness="1" BorderBrush="White" >
                    <Grid Height="76">
                        <TextBlock VerticalAlignment="Top" HorizontalAlignment="Left" Text="{Binding Day}"  Style="{StaticResource PhoneTextLargeStyle}"/>
                        <TextBlock VerticalAlignment="Bottom" HorizontalAlignment="Right" Text="{Binding DayMalayalam}"  FontSize="16"/>
                    </Grid>
                </Border>
            </Grid>
        </DataTemplate>
        <DataTemplate x:Key="HorizontalPivotTemplate">
            <Grid  MinWidth="65" local:ItemsGridLayout.GridRow="{Binding Week}" 
                                        local:ItemsGridLayout.GridColumn="{Binding WeekDay}">
                <Border BorderThickness="1" BorderBrush="White" >
                    <Grid Height="56">
                        <TextBlock VerticalAlignment="Top" HorizontalAlignment="Left" Text="{Binding Day}"  Style="{StaticResource PhoneTextLargeStyle}"/>
                        <TextBlock VerticalAlignment="Bottom" HorizontalAlignment="Right" Text="{Binding DayMalayalam}"  FontSize="16"/>
                    </Grid>
                </Border>
            </Grid>
        </DataTemplate>
    </phone:PhoneApplicationPage.Resources>

现在,我的要求是在这些模板之间进行选择,并在手机的方向发生变化时将其分配给ItemControl的ItemTemplate。为此,我使用了电话申请页面的OrientationChanged事件。

private void PhoneApplicationPage_OrientationChanged_1(object sender, OrientationChangedEventArgs e)
        {
            if (e.Orientation == PageOrientation.PortraitDown || e.Orientation == PageOrientation.PortraitUp)
            {
                //Assign VerticalPivotTemplate as CalendarControl's ItemTemplate
            }
            else
            {
               //Assign HorizontalPivotTemplate as CalendarControl's ItemTemplate
            }
        }

我不知道在活动中写什么来达到我的要求。我无法在此处访问CalendarControl ItemControl。请有人帮帮我

1 个答案:

答案 0 :(得分:0)

我不禁注意到您只是在DataTemplate中更改网格宽度,为什么不使用触发器来执行此操作而不是更改整个模板..

        <Grid.Style>
            <Style TargetType="Grid">
                <Setter Property="Width" Value="56"/>
                <Style.Triggers>
                    <DataTrigger Binding="{Binding Orientation, RelativeSource={RelativeSource AncestorType={x:Type phone:PhoneApplicationPage}}}" Value="PortraitDown">
                        <Setter Property="Width" Value="76"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </Grid.Style>