【问题标题】:DataTemplate depending on Page Orientation not changing WP7DataTemplate 取决于页面方向不改变 WP7
【发布时间】:2015-01-13 12:59:52
【问题描述】:

我需要一个列表框来根据页面方向显示不同的信息,所以开始寻找并找到了这个answer,实现如下:

在资源部分定义了 DataTemplates:

    <DataTemplate x:Key="LandscapeTemplate">
        <StackPanel Orientation="Horizontal">
            <TextBlock Name="txtNombre" Text="{Binding Path=nombre}" TextWrapping="Wrap" Padding="0,10,0,0" FontSize="28" VerticalAlignment="Bottom"></TextBlock>
            <TextBlock Name="txtSep" Text=" - " TextWrapping="Wrap" Padding="0,10,0,0" FontSize="22" VerticalAlignment="Bottom"></TextBlock>
            <TextBlock Name="txtFecha" Text="{Binding Path=fecha_hora}" TextWrapping="Wrap" Padding="0,10,0,0" FontSize="22" VerticalAlignment="Bottom"></TextBlock>
        </StackPanel>
    </DataTemplate>
    <DataTemplate x:Key="PortraitTemplate">
        <StackPanel Orientation="Horizontal">
            <TextBlock Name="txtNombre" Text="{Binding Path=nombre}" TextWrapping="Wrap" Padding="0,10,0,0" FontSize="28" VerticalAlignment="Bottom"></TextBlock>
        </StackPanel>
    </DataTemplate>

然后使用VisualStateManager如下:

<VisualStateManager.VisualStateGroups>
    <VisualStateGroup x:Name="OrientationStates">
        <VisualState x:Name="Landscape">
            <Storyboard>
                <ObjectAnimationUsingKeyFrames Storyboard.TargetName="lista" 
                                                       Storyboard.TargetProperty="ItemTemplate">
                    <DiscreteObjectKeyFrame KeyTime="0" 
                                            Value="{StaticResource LandscapeTemplate}"/>
                </ObjectAnimationUsingKeyFrames>
            </Storyboard>
        </VisualState>
    </VisualStateGroup>
</VisualStateManager.VisualStateGroups>

我的应用程序的默认方向是纵向,所以我最初将 PortraitTemplate 分配给我的列表框

<ListBox x:Name="lista" 
                 SelectionChanged="ListBox_SelectionChanged" 
                 ItemTemplate="{StaticResource PortraitTemplate}">
        </ListBox>

然后在测试结果时,模板不会改变。我想知道我错过了什么?或者我还需要添加什么来更改模板

提前致谢。

附加信息:还尝试使用此info 实现我所需要的

【问题讨论】:

  • 你页面的基类是 LayoutAwarePage 吗?正如您所遵循的答案的最后一行所指出的那样。
  • 我试图检查,但不知道如何检查。

标签: c# xaml windows-phone-7


【解决方案1】:

终于奏效了:

在应用程序页面构造函数中(在本例中为 MainPage):

            this.OrientationChanged += new EventHandler<OrientationChangedEventArgs>(MainPage_OrientationChanged); 

并添加处理方法:

void MainPage_OrientationChanged(object sender, OrientationChangedEventArgs e)
    {
        if ((e.Orientation & PageOrientation.Landscape) > 0)
        {
            VisualStateManager.GoToState(this, "Landscape", true);
            lista.ItemTemplate = LandscapeTemplate();
            lista.ItemsSource = null;
            List<StrTutoria> eventos = generarLista(asistiendo);
            this.lista.ItemsSource = eventos;
        }
        else
        {
            VisualStateManager.GoToState(this, "Portrait", true);
            // Here goes code for portrait state
        }
    }

和 LandscapeTemplate() 实现:

private DataTemplate LandscapeTemplate()
    {
        string xaml =
            @"<DataTemplate
                    xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
                    xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"">
                    <StackPanel Orientation=""Horizontal"">
                        <TextBlock Name=""txtNombre"" Text=""{Binding Path=nombre}"" TextWrapping=""Wrap"" Padding=""0,10,0,0"" FontSize=""28"" VerticalAlignment=""Bottom""></TextBlock>
                        <TextBlock Name=""txtSep"" Text="" - "" TextWrapping=""Wrap"" Padding=""0,10,0,0"" FontSize=""22"" VerticalAlignment=""Bottom""></TextBlock>
                        <TextBlock Name=""txtFecha"" Text=""{Binding Path=fecha_hora}"" TextWrapping=""Wrap"" Padding=""0,10,0,0"" FontSize=""22"" VerticalAlignment=""Bottom""></TextBlock>
                    </StackPanel>
                </DataTemplate>";
        DataTemplate dt = (DataTemplate)XamlReader.Load(xaml);
        return dt;
    }

解决方案的最后一部分可用here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多