精通Silverlight——12.4.8 ScrollViewer滚动查看器控件

来源:互联网 发布:w网络电视 编辑:程序博客网 时间:2024/06/05 06:48

  为了能显示滚动内容,应该使用ScrollView控件,通常的声明方式如下面的XAML代码所示。

<Canvas

        xmlns="http://schemas.microsoft.com/client/2007"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"        xmlns:uicontrol="clr-namespace:Silverlight.Samples.Controls;assembly=ClientBin/Silverlight.Samples.Controls.dll"        

        x:Name="parentCanvas"

        Loaded="Page_Loaded"

        x:Class="ScrollViewerDemo.Page;assembly=ClientBin/ScrollViewerDemo.dll"        

        Width="640"

        Height="480"

        Background="White"

        >

  <!-- 定义ScrollViewer对象,需要在后置代码中指定显示区域 -->

  <uicontrol:ScrollViewer x:Name="scrollViewer" ScrollableHeight="60" ScrollableWidth="2"  Height="200" Width="600"/>

  <!--用于ScrollViewer的内容区域的画布-->

  <Canvas x:Name="standinCanvas" Width="1200" Height="1200">

    <!--定义一个大的文本块-->

    <TextBlock Width="400" Height="1200" Canvas.Left="0" Canvas.Top="0" TextWrapping="Wrap">

      <Run Text="Try Visual Studio 2008"/>

      <LineBreak/>

      <Run Text="Microsoft Visual Studio 2008 provides an industry-leading developer experience for Windows Vista, the 2007 Microsoft Office system, and the Web. In addition, it continues in the Microsoft tradition of development language innovation. To enable evaluation of Visual Studio 2008, this page provides links to trial versions of Visual Studio 2008. For more information, see Visual Studio 2008 Product Information."/>

      <LineBreak/>

      <Run Text=""/>

      <LineBreak/>

      <Run Text="90-Day Trial Downloads"/>

      <LineBreak/>

      <Run Text="Visual Studio 2008 Professional Edition"/>

      <LineBreak/>

      <Run Text="Visual Studio Team System 2008 Team Suite"/>

      <LineBreak/>

      <Run Text="Visual Studio Team System 2008 Team Foundation Server"/>

      <LineBreak/>

      <Run Text="Visual Studio Team System 2008 Test Load Agent"/>

      <LineBreak/>

      <Run Text="Current MSDN Professional and Premium Subscribers can download Visual Studio 2008 from MSDN Subscriber Downloads."/>

      <LineBreak/>

      <Run Text=""/>

      <LineBreak/>

      <Run Text="In addition, you can download the free Visual Studio 2008 Express Editions and Microsoft .NET Framework 3.5."/>

      <LineBreak/>

      <Run Text=""/>

    </TextBlock>

  </Canvas>

</Canvas>

为了使ScrollViewer和画布StandinCanvas关联起来,需要使用后置代码文件设置Content属性。后置代码如下所示。

            //必须先从根画布中将这个用于ScrollViewer内容的画布移除,以形成一个独立的画布。

            ((Canvas)(standinCanvas.Parent)).Children.Remove(standinCanvas);

            //将这个独立的画布作为SrollViewer的内容。

            scrollViewer.Content = standinCanvas;

代码先将根画布中的子画布移除,使这个画布对象成为一个独立的画布,然后将ScrollViewerContent属性指向这个独立的画布。

这个示例程序的运行效果如图所示。