ListBox ItemTemplate alignment错误

时间:2014-03-11 03:39:59

标签: c# xaml windows-phone-8 listbox datatemplate

我有一个很烦人的错误。所以我所做的就是我创建了自己的ListBox ItemTemplate。 XAML就是:

<DataTemplate x:Key="DataTemplate1">
        <StackPanel toolkit:TiltEffect.IsTiltEnabled="True" Orientation="Vertical" Width="456" Height="115">
            <TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="{Binding AccountTitle}" VerticalAlignment="Top" Height="40" Width="446" FontFamily="Segoe WP" FontSize="29" Margin="10,0,0,0"/>
            <TextBlock HorizontalAlignment="Right" TextWrapping="Wrap" Text="{Binding AccountDate}" VerticalAlignment="Center" FontFamily="Segoe WP" FontSize="20" TextAlignment="Right" Margin="0,2,0,0">
                <TextBlock.Foreground>
                    <SolidColorBrush Color="{StaticResource PhoneTextMidContrastColor}"/>
                </TextBlock.Foreground>
            </TextBlock>
            <TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="{Binding AccountUsername}" VerticalAlignment="Top" Height="40" Width="446" FontFamily="Segoe WP" FontSize="26" Margin="10,-33,0,0">
                <TextBlock.Foreground>
                    <SolidColorBrush Color="{StaticResource PhoneTextMidContrastColor}"/>
                </TextBlock.Foreground>
            </TextBlock>
            <TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="{Binding AccountWebsite}" VerticalAlignment="Top" Height="40" Width="446" FontFamily="Segoe WP" FontSize="22" Margin="10,-9,0,0">
                <TextBlock.Foreground>
                    <SolidColorBrush Color="{StaticResource PhoneTextLowContrastColor}"/>
                </TextBlock.Foreground>
            </TextBlock>
            <TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="{Binding FolderID}" VerticalAlignment="Top" Height="40" Width="446" FontFamily="Segoe WP" FontSize="29" Margin="10,0,0,0" Visibility="Collapsed"/>
        </StackPanel>
    </DataTemplate>

现在。当我运行应用程序时。它看起来像这样:

Everything works fine

但是当我倾斜设备并进入风景时,会发生这种情况:

This is annoying.

有人可以告诉我如何解决这个问题。因此无论它处于什么方向,它都会适应并使用整个列表框。

谢谢! 如果您需要我提供任何代码,请询问,我会提供。

1 个答案:

答案 0 :(得分:1)

尝试将ListBoxItem的HorizontalContentAlignment属性设置为Stretch

<ListBox>
    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
        </Style>
    </ListBox.ItemContainerStyle>
</ListBox>

更新:

除了上述步骤,请从Width中删除硬编码的StackPanel

<DataTemplate x:Key="DataTemplate1">
    <StackPanel toolkit:TiltEffect.IsTiltEnabled="True" Orientation="Vertical" Height="115">
        ........
        ........
    </StackPanel>
</DataTemplate>