Windows Phone 8 - Center PanoramaItem.Header

岁酱吖の 提交于 2019-12-23 20:16:02

问题


I'm developing a Panorama App for Windows Phone 8 and I got a small problem. I added an image to the header of one of my Panorama.Item (and it shows up fine) but I'd love it to be centered and not aligned left. I don't even know if it's possible or if you can't change the alignment at all. Here's the code for the header I'm talking about:

<phone:PanoramaItem.Header>
   <Image x:Name="Logo" Height="100" HorizontalAlignment="Center" Tap="Logo_Tap" />
</phone:PanoramaItem.Header>

The HorizontalAlignment="Center"-Property unfortunately doesn't seem to do it. Any help would be appreciated.


回答1:


The simplest way is:

<phone:PanoramaItem.Header>     
                        <Grid Width="432">
                            <Image x:Name="Logo"
                                   Height="100"
                                   HorizontalAlignment="Center"
                                   Tap="Logo_Tap" />
                        </Grid>
</phone:PanoramaItem.Header>



回答2:


I would avoid using a hard coded width. The proper way to do this would be to override the Template on the PanoramaItem, like this (They key part being changing the HorizontalAlignment on the header ContentControl)

<ControlTemplate x:Key="CenteredHeaderPanoramaItemTemplate" TargetType="phone:PanoramaItem">
    <Grid Background="{TemplateBinding Background}" Margin="12,0,0,0">
        <Grid.RowDefinitions>
            <RowDefinition Height="auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <ContentControl x:Name="header" CharacterSpacing="-35" ContentTemplate="{TemplateBinding HeaderTemplate}" Content="{TemplateBinding Header}" FontSize="66" FontFamily="{StaticResource PhoneFontFamilySemiLight}" Margin="12,-2,0,38" HorizontalAlignment="Stretch">
            <ContentControl.RenderTransform>
                <TranslateTransform x:Name="headerTransform"/>
            </ContentControl.RenderTransform>
        </ContentControl>
        <ContentPresenter Content="{TemplateBinding Content}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" Grid.Row="1" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
    </Grid>
</ControlTemplate>

Then just assign the Template using a StaticResource

<phone:PanoramaItem Template="{StaticResource CenteredHeaderPanoramaItemTemplate}">
    <phone:PanoramaItem.Header>
        <Image x:Name="Logo" Height="100" HorizontalAlignment="Center" Tap="Logo_Tap" />
    </phone:PanoramaItem.Header>
    <Grid x:Name="PanoramaItemContent">
    </Grid>
</phone:PanoramaItem>



回答3:


<phone:PanoramaItem.Header>
<Grid  HorizontalAlignment="Center">
   <Image x:Name="Logo" Height="100" Tap="Logo_Tap" />
</Grid>
</phone:PanoramaItem.Header>

try this



来源:https://stackoverflow.com/questions/20424516/windows-phone-8-center-panoramaitem-header

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!