天天看点

Windows Phone 7 水平滚动的文本

有木有发现应用的标题长一点就显示不全鸟,滚动一下就可以了。有两种方法一种是使用ScrollViewer控件,另外一种是使用TranslateTransform平移变换来实现。

一、ScrollViewer控件直接设置HorizontalScrollBarVisibility="Auto"就可以水平滚了

<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28"> 

            <TextBlock x:Name="ApplicationTitle" Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}"/> 

            <ScrollViewer Width="480" 

         HorizontalScrollBarVisibility="Auto"><TextBlock x:Name="PageTitle" Text="这个名字好像有点长" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/> 

            </ScrollViewer> 

        </StackPanel> 

二、使用TranslateTransform平移变换来实现,使用这种方法就不会产生ScrollViewer的那种滚动效果,就是你把文字拨到哪,它就定在哪。

 <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">

<StackPanel x:Name="ContentPanel2" Background="Black"

                            ManipulationDelta="ContentPanel2_ManipulationDelta" Margin="0,0,-941,447">

                <TextBlock Text="这个也有点长,但是没有惯性滚动的。" Foreground="White" FontSize="100" />

                </StackPanel>

        </Grid>

public partial class MainPage : PhoneApplicationPage  

    {  

        //创建一个平移变换的对象  

        private TranslateTransform transform = new TranslateTransform();  

        public MainPage()  

        {  

            InitializeComponent();  

            //设置StackPanel的变换属性为平移变换  

            ContentPanel2.RenderTransform = transform;  

        }  

        private void ContentPanel2_ManipulationDelta(object sender, ManipulationDeltaEventArgs e)  

            //X轴移动  

            transform.X +=e.DeltaManipulation.Translation.X;  

    } 

本文转自linzheng 51CTO博客,原文链接:http://blog.51cto.com/linzheng/1078559

继续阅读