Silverlight Canvas on Scrollviewer not triggering

笑着哭i 提交于 2020-01-03 00:21:06

问题


Why does this work so well in wpf

<ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
<Canvas x:Name="MyDesigner">
</Canvas>
</ScrollViewer>

Now when I do the same thing in silverlight and load an control "that can be dragged" the scrollbars are not triggered, when I drag out of view, nothing happens... but in wpf it automatically shows them...


回答1:


Here is the solution I found. The canvas can grow dynamically, but you will need to explicitly set the height to a new value. So, if you have 20 textblocks with a height of 21, you need to set the canvas height as: Canvas.Height = 22.0 * 100; for the scrollviewer to pick up the new height.

MainPage.xaml

<Canvas Canvas.Left="5" Canvas.Top="25"  >
    <ScrollViewer Width="300" Height="700"  x:Name="CanSummaryScroller">
        <Canvas x:Name="canSummaryCells"  >
        </Canvas>
    </ScrollViewer>  
</Canvas>

MainPage.xaml.cs

boxDevSumList is a List of TextBoxes

for (int i = 0; i < 100; i++)
{
    boxDevSumList.Add(new Cell());
    (boxDevSumList.ElementAt(i)).Width = 271;
    (boxDevSumList.ElementAt(i)).Height = 21;
    (boxDevSumList.ElementAt(i)).SetValue(FontFamilyProperty, new FontFamily("Calibri"));
    (boxDevSumList.ElementAt(i)).FontSize = 12;
    (boxDevSumList.ElementAt(i)).Text = "this is a test";
    if (i.Equals(1) || i.Equals(3) || i.Equals(5) || i.Equals(7) || i.Equals(9))
        (boxDevSumList.ElementAt(i)).Background = lgBrush;
    (boxDevSumList.ElementAt(i)).Opacity = .9;
    canSummaryCells.Children.Add(boxDevSumList.ElementAt(i));
    boxDevSumList.ElementAt(i).SetValue(Canvas.TopProperty, (double)(i * 21) + 45);
    boxDevSumList.ElementAt(i).SetValue(Canvas.LeftProperty, 4.0);
    canSummaryCells.Height = 22.0 * 100;
}



回答2:


As a quick check against the normal 'gotcha' have you explicitly set the canvas height / width properties?

If I knock up some xaml for test purposes and run it:

 <ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
     <Canvas x:Name="test" Background="Beige">
         <TextBlock Canvas.Left="2000" Canvas.Top="200" Text="test"/>
     </Canvas> 
 </ScrollViewer>

Will not show a scroll bar, even though I have explicitly created content in the canvas 2000 to the left, the canvas width not being set means the scroll viewer has no range to bind to so to speak. The canvas without a width is considered is just infinitely wide from what I can see. Whilst this is not the same as dragging the concept of putting a piece of content outside of the current view is there.

As soon as you add a width, it defines a finite area to scroll on and the scroll bar shows up.



来源:https://stackoverflow.com/questions/1052272/silverlight-canvas-on-scrollviewer-not-triggering

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