XAML:当子对象的ScaleTransform变大时,使ScrollViewer显示滚动条 [英] XAML: make a ScrollViewer show scrollbars when the ScaleTransform of a child object gets big
问题描述
我正在Silverlight 3应用程序中对某些文档进行打印预览"控件.我在ScrollViewer内有一个Canvas(用于显示文档),并且具有放大/缩小"按钮,用于控制Canvas.RenderTransform属性的ScaleTransform的X和Y Scale属性.我希望当我放大"足够以使画布不再在ScrollViewer区域中可见时显示ScrollViewer的滚动条,但是似乎它们仅根据Canvas本身的宽度/高度显示.是否放大.
I am making a sort of "print preview" control for some documents in my Silverlight 3 application. I have a Canvas (for showing the document) inside of a ScrollViewer, and I have zoom in / zoom out buttons that control the X and Y Scale properties of the ScaleTransform for the Canvas.RenderTransform property. I want the scrollbars of the ScrollViewer to show up when I "zoom in" enough such that the canvas is no longer visible in the ScrollViewer area, but it seems that they only show up depending on the Width/Height of the Canvas itself, regardless of whether it is zoomed in or not.
任何人都可以帮忙吗?
推荐答案
是的,问题在于Silverlight中没有LayoutTransform
. 此处列出了一些解决此问题的方法.
Yeah, the problem is that there is no LayoutTransform
in Silverlight. There are some workarounds to this problem listed here.
这里的想法是提供一个调整大小的中间画布,从而调整可滚动区域的大小.例如,如果我具有以下XAML:
The idea here is to provide an intermediate canvas that gets resized and as a result, resizes the scrollable area. For example, if I have the following XAML:
<Grid x:Name="LayoutRoot">
<Grid.RowDefinitions>
<RowDefinition Height="200" />
<RowDefinition Height="25" />
</Grid.RowDefinitions>
<ScrollViewer Grid.Row="0" x:Name="sc" VerticalScrollBarVisibility="Auto"
HorizontalScrollBarVisibility="Auto" Width="200" Height="200" >
<Canvas x:Name="sizer" Width="200" Height="200">
<Rectangle x:Name="gradientRect" Width="200" Height="200">
<Rectangle.RenderTransform>
<ScaleTransform ScaleX="1" ScaleY="1"/>
</Rectangle.RenderTransform>
<Rectangle.Fill>
<LinearGradientBrush StartPoint="0,0" EndPoint="1,1">
<GradientStop Color="Red" Offset="0.1"/>
<GradientStop Color="Yellow" Offset="0.5"/>
<GradientStop Color="Red" Offset="0.9"/>
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
</Canvas>
</ScrollViewer>
<Button Grid.Row="1" Content="Multiply by Two" Click="ScaleRect" Width="100" Height="25"></Button>
</Grid>
您会注意到,我将<Canvas x:Name="sizer"/>
放在了<ScrollViewer/>
和<Rectangle/>
之间,并且将ScaleRect
的单击事件放在了<Button/>
中.
You'll notice that I put the <Canvas x:Name="sizer"/>
between the <ScrollViewer/>
and <Rectangle/>
and the click event of ScaleRect
in the <Button/>
.
ScaleRect
子项仅将矩形缩放2.然后使用该值更改sizer
,Width
和Height
,从而更新ScrollViewer的滚动条.这是ScaleRect
的子项:
The ScaleRect
sub simply scales the rectangle by 2. That value is then used to change the sizer
Width
and Height
, thus updating the ScrollViewer's scrollbars. Here's the sub for ScaleRect
:
Private Sub ScaleRect(ByVal sender As Object, ByVal e As RoutedEventArgs)
Dim zoom As Double = 2.0
Dim scaleX = gradientRect.RenderTransform.GetValue(ScaleTransform.ScaleXProperty)
Dim scaleY = gradientRect.RenderTransform.GetValue(ScaleTransform.ScaleYProperty)
gradientRect.RenderTransform.SetValue(ScaleTransform.ScaleXProperty, scaleX * zoom)
gradientRect.RenderTransform.SetValue(ScaleTransform.ScaleYProperty, scaleY * zoom)
sizer.Height *= zoom
sizer.Width *= zoom
End Sub
这篇关于XAML:当子对象的ScaleTransform变大时,使ScrollViewer显示滚动条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!