简体   繁体   English

如何在ScrollViewer(WP7)中执行RenderTransform?

[英]How can I perform RenderTransform within ScrollViewer (WP7)?

I've tried many ways to manage RenderTransform within ScrollViewer (Windows Phone 7 Silverlight), but it seems to me almost impossible now. 我已经尝试了许多方法来在ScrollViewer(Windows Phone 7 Silverlight)中管理RenderTransform,但是在我看来现在几乎是不可能的。 What I got is Grid with with its sizes inside ScrollViewer and I want to change grid's content size from code by RenderTransform by it do nothing! 我得到的是在ScrollViewer中具有其大小的Grid,我想通过RenderTransform从代码更改网格的内容大小,因为它什么也不做!

      <ScrollViewer x:Name="scrollViewer" Width="800" Height="480" HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Visible">
        <Grid x:Name="grid" Width="1600" Height="960" HorizontalAlignment="Left" VerticalAlignment="Top">
            <Grid.RenderTransform>
                <CompositeTransform x:Name="scaleTransform" ScaleX="1" ScaleY="1"/>
            </Grid.RenderTransform>
            <Image x:Name="backgroundImage" Source="/Images/backgrounds/Happy rainbow.png" Stretch="Fill"/>
        </Grid>
    </ScrollViewer>

In code: 在代码中:

        private void button_Click(object sender, RoutedEventArgs e)
    {
            (grid.RenderTransform as CompositeTransform ).CenterX = 0;
            (grid.RenderTransform as CompositeTransform ).CenterY = 0;
            (grid.RenderTransform as CompositeTransform ).ScaleX = 0.5;
            (grid.RenderTransform as CompositeTransform ).ScaleY = 0.5;
            grid.UpdateLayout();
    }

Binding on Scale and Visual states do nothig too. 缩放和视觉状态绑定也不会显示。 I really would appreciate your help. 非常感谢您的帮助。

Better idea... put your Grid content into an ItemsControl, and perform a ScaleTransform on the ItemsControl. 更好的主意...将网格内容放入ItemsControl,然后在ItemsControl上执行ScaleTransform。

<Grid>
    <ItemsControl x:Name="ContentScaler">
        <Image x:Name="backgroundImage" Source="/Images/backgrounds/Happy rainbow.png" Stretch="Fill"/>
    </ItemsControl>
</Grid>

And in the code-behind... 然后在代码背后

ContentScaler.RenderTransform = new ScaleTransform() { ScaleX = 0.5, ScaleY = 0.5, CenterX = 0, CenterY = 0 };

Depending on what else you may need to do, you may need to do something like set a WrapPanel as the ItemsPanelTemplate and/or resize the ItemsControl when you do your scaling. 根据您可能需要执行的其他操作,您可能需要执行诸如将WrapPanel设置为ItemsPanelTemplate和/或在缩放时调整ItemsControl的大小之类的操作。 It can get a little tricky, but hopefully this will get you pointed in the right direction. 可能会有些棘手,但是希望这可以使您指向正确的方向。

The use of Grid in Silverlight tends to be a bit overused also, IMHO, unless one needs to break things into a table type layout. 恕我直言,在Silverlight中使用Grid也会被过度使用,除非需要将内容分解为表格类型的布局。 A Canvas may be better suited for what you are doing. 画布可能更适合您的工作。

I'm not sure if this is exactly what you need but if you insert an extra grid like this... 我不确定这是否正是您需要的,但是如果您插入这样的额外网格...

            <ScrollViewer Grid.Row="1" x:Name="scrollViewer"
                      Width="800"
                      Height="480"
                      HorizontalScrollBarVisibility="Visible"
                      VerticalScrollBarVisibility="Visible">
            <Grid x:Name="grid"
                  HorizontalAlignment="Left"
                  VerticalAlignment="Top">
                <Grid
                    Width="1600"
                    Height="960">
                    <Grid.RenderTransform>
                        <CompositeTransform x:Name="scaleTransform"
                                            ScaleX="1"
                                            ScaleY="1" />
                    </Grid.RenderTransform>
                    <Image x:Name="backgroundImage"
                           Source="ApplicationIcon.png"
                           Stretch="Fill" />
                    </Grid>
            </Grid>
        </ScrollViewer>

at least the contents will get scaled.. 至少内容会按比例缩放。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM