簡體   English   中英

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

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

我已經嘗試了許多方法來在ScrollViewer(Windows Phone 7 Silverlight)中管理RenderTransform,但是在我看來現在幾乎是不可能的。 我得到的是在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>

在代碼中:

        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();
    }

縮放和視覺狀態綁定也不會顯示。 非常感謝您的幫助。

更好的主意...將網格內容放入ItemsControl,然后在ItemsControl上執行ScaleTransform。

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

然后在代碼背后

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

根據您可能需要執行的其他操作,您可能需要執行諸如將WrapPanel設置為ItemsPanelTemplate和/或在縮放時調整ItemsControl的大小之類的操作。 可能會有些棘手,但是希望這可以使您指向正確的方向。

恕我直言,在Silverlight中使用Grid也會被過度使用,除非需要將內容分解為表格類型的布局。 畫布可能更適合您的工作。

我不確定這是否正是您需要的,但是如果您插入這樣的額外網格...

            <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>

至少內容會按比例縮放。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM