2014-01-28 22 views
0

我们有一个绝对布局的要求,允许滚动它的内容(子控件)。布局和所有的Child控件都是在应用程序运行期间随时创建和添加的;因此,XAML不适用于创建布局。WP8画布内滚动查看器 - 必须代码隐藏

不幸的是,使用后面的代码很难实现所需的滚动行为;与XAML不同,我们可以非常简单地实现它。

下面的XAML演示所需的行为

<phone:PhoneApplicationPage 
    x:Class="TestWP8App.MainPage" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone" 
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    mc:Ignorable="d" 
    FontFamily="{StaticResource PhoneFontFamilyNormal}" 
    FontSize="{StaticResource PhoneFontSizeNormal}" 
    Foreground="{StaticResource PhoneForegroundBrush}" 
    SupportedOrientations="Portrait" Orientation="Portrait" 
    shell:SystemTray.IsVisible="True"> 
<ScrollViewer HorizontalAlignment="Left" VerticalAlignment="Top" Width="480"> 
    <Canvas Height="1500"> 
     <Button Content="1" Height="300" Canvas.Left="10" Canvas.Top="10" Width="120"/> 
     <Button Content="2" Height="300" Canvas.Left="10" Canvas.Top="110" Width="120"/> 
     <Button Content="3" Height="300" Canvas.Left="10" Canvas.Top="210" Width="120"/> 
     <Button Content="4" Height="300" Canvas.Left="10" Canvas.Top="310" Width="120"/> 
     <Button Content="5" Height="300" Canvas.Left="10" Canvas.Top="410" Width="120"/> 
    </Canvas> 
</ScrollViewer> 
</phone:PhoneApplicationPage> 

通过上述特性转换为C#代码,这是可能达到相同的“看”,只有内容从未滚动。

有谁知道如何通过代码隐藏实现相同的结果吗?或者知道一个例子的位置,完全是这样使用代码隐藏的吗?

回答

0

问题是父子关系的问题。

最初我有一个ScrollViewer(SV)和一个主画布(MC)。

问题在于即使将MC设置为SV的内容,MC也被添加为其他容器的子项。

的解决方案是重新设置父级SV在第三画布(3C)使层次[父 - >子]:

3C - > SV - > MC

宽度,然后高度可以被应用到3C & SV,让MC根据其内容自动调整大小。

由于MC获得的尺寸大于SV & 3C,因此允许滚动。

所有的孩子都加入到MC专门

0

它很容易

  ScrollViewer sViewer = new ScrollViewer() {HorizontalAlignment=HorizontalAlignment.Left,VerticalAlignment=VerticalAlignment.Top,Width480 }; 
      Canvas cc = new Canvas() { Height=1500}; 
      //same for all the buttons 
      Button b1 = new Button() { Height=300,Width=120,Content="1"}; 
      Canvas.SetLeft(b1, 10); 
      Canvas.SetTop(b1, 10); 
      //adding the canvas into sViewer 
      sViewer.Content = cc; 

希望它可以帮助

0

XAML代码转换成C#代码以直接的方式。以下是将创建上述XAML的C#代码片段:

var scrollViewer = new ScrollViewer 
{ 
    HorizontalAlignment = HorizontalAlignment.Left, 
    VerticalAlignment = VerticalAlignment.Top, 
    Width = 480 
}; 

var canvas = new Canvas 
{ 
    Height = 1500 
}; 

scrollViewer.Content = canvas; 

for (var i = 1; i <= 5; ++i) 
{ 
    var button = new Button 
    { 
     Width = 120, 
     Height = 300 
     Content = i.ToString() 
    }; 
    Canvas.SetLeft(button, 10); 
    Canvas.SetTop(button, (i - 1) * 100 + 10)); 
    canvas.Children.Add(button); 
} 
+0

感谢您的答复,但有些事必须是不同的,它是仿佛ScrollPanel已经成长为配合画布的大小,因此一直推关闭屏幕。如果我玩的尺寸,我也可以看到按钮'溢出'容器的边界,当我希望ScrollViewer'剪辑'它的可视窗口外的任何子控件。关于ScrollViewer设置高度/宽度的规则是什么?它应该留给自己的大小? – Gov