I'm trying to implement zoom in/out functionality in a ScrollViewer in WP7. Inside my scrollviewer is a usercontrol, which consists of a grid and some ellipsis in variaous cells.
Implementing no zoom, I am able to run the app in the emulator and scroll the scrollviewer up/down, left/right.
My initial thought was to use a scale transform on the scrollviewer:
<ScrollViewer x:Name="svScale"
Margin="0"
Grid.Row="1"
HorizontalScrollBarVisibility="Visible">
<ScrollViewer.RenderTransform>
<ScaleTransform ScaleX="2" ScaleY="2"/>
</ScrollViewer.RenderTransform>
<ScrollViewer.RenderTransformOrigin>
<Point X=".5" Y=".5"/>
</ScrollViewer.RenderTransformOrigin>
<local:MyControl x:Name="test"
Width="486"
Height="56">
</local:MyControl>
</ScrollViewer>
The problem is that when I scroll around, the scrollviewer always scrolls the contents back to the vertical and horizontal center.
So I tried applying the scale transform to the user control:
<ScrollViewer x:Name="svScale"
Margin="0"
Grid.Row="1"
HorizontalScrollBarVisibility="Visible">
<local:MyControl x:Name="test"
Width="486"
Height="56"
SelectedKey="{Binding SelectedKey}"
SelectedScale="{Binding SelectedScale}">
<local:MyControl.RenderTransform>
<ScaleTransform ScaleX="2" ScaleY="2"/>
</local:MyControl.RenderTransform>
<local:MyControl.RenderTransformOrigin>
<Point X=".5" Y=".5"/>
</local:MyControl.RenderTransformOrigin>
</local:MyControl>
</ScrollViewer>
But when I apply the scale transform to the user control, it doesn't appear any larger than it normally does, regardless of what the ScaleX and ScaleY values are. What is going on here?
Related
I know how to implement it using Grid and StackPanel, can it done here without that?
Sure, just align it left and right with panel! For example:
Horizontal stretch
<RelativePanel>
<TextBox RelativePanel.AlignLeftWithPanel="True"
RelativePanel.AlignRightWithPanel="True" />
</RelativePanel>
If you want vertical stretch, do the similar thing, just align the top and bottom with panel:
Vertical stretch
<RelativePanel>
<TextBox RelativePanel.AlignTopWithPanel="True"
RelativePanel.AlignBottomWithPanel="True" />
</RelativePanel>
I want to display images that the user has provided. These images are likely to be larger than the screen resolution (so need zoom and pan capability) plus the image may not be orientated correctly for the screen (so need to be able to rotate).
Implementing pan and zoom seems to be somewhat straightforward:
<ScrollViewer HorizontalSnapPointsType="None" HorizontalScrollBarVisibility="Auto" VerticalSnapPointsType="None" ZoomSnapPointsType="None" IsHorizontalRailEnabled="False" IsVerticalRailEnabled="False" ManipulationMode="All" VerticalScrollBarVisibility="Auto">
<Image x:Name="pannableImage" Source="{Binding FullSizedImage}" AutomationProperties.Name="{Binding Title}" />
</ScrollViewer>
This works well and meets my needs, although I would like to be able to set the initial zoom factor so that if the image is larger than the screen, the zoom factor is set so that it fills the screen and if the image isn't larger than the screen, the zoom factor is set so that the image is shown at its full size, i.e. not zoomed in.
However, I'm struggling to get rotation to work acceptably. I've tried this:
<ScrollViewer HorizontalSnapPointsType="None" HorizontalScrollBarVisibility="Auto" VerticalSnapPointsType="None" ZoomSnapPointsType="None" IsHorizontalRailEnabled="False" IsVerticalRailEnabled="False" ManipulationMode="All" VerticalScrollBarVisibility="Auto">
<Image x:Name="pannableImage" Source="{Binding FullSizedImage}" AutomationProperties.Name="{Binding Title}" >
<Image.Projection>
<PlaneProjection RotationZ="{Binding ImageRotation}"/>
</Image.Projection>
</Image>
</ScrollViewer>
Although this does rotate the image, the problem is that the ScrollViewer then gets the scrolling wrong. I've also tried putting the Projection onto the ScrollViewer instead of the Image and that is even worse.
Putting the project onto the image seems to make the most sense since the ScrollViewer should then get the dimensions of the projected image but that doesn't quite seem to be the case.
What am I misunderstanding here, please?
Thanks.
Philip
The solution was to use a RenderTransform instead of a Projection:
<Image x:Name="pannableImage" Source="{Binding FullSizedImage}" ManipulationMode="All" Loaded="pannableImage_Loaded" IsDoubleTapEnabled="False" IsHitTestVisible="False" IsHoldingEnabled="False" IsRightTapEnabled="False" IsTapEnabled="False" ScrollViewer.VerticalScrollBarVisibility="Disabled" LayoutUpdated="pannableImage_LayoutUpdated">
<Image.RenderTransform>
<TransformGroup>
<ScaleTransform x:Name="Scale" />
<RotateTransform x:Name="Rotate" />
<TranslateTransform x:Name="Translate" />
</TransformGroup>
</Image.RenderTransform>
</Image>
I have a horizontal ScrollView implemented and working. however, when I Scrol horizontally he walks, but when he returns to deslargo initial position no reason for it.
Anyone know a solution?
Thanks
<ScrollViewer ScrollViewer.HorizontalScrollBarVisibility="Visible" ScrollViewer.VerticalScrollBarVisibility="Disabled" Height="50" Width="Auto">
<StackPanel Name="sp_urlsHome" Orientation="Horizontal" Height="50" Width="Auto">
<TextBlock Text="adasdasdsasdfffffffffffffffffffffffffsdfsdfsdfsdfsfsdfdas"/>
</StackPanel>
</ScrollViewer>
You must set right ScrollViewer Width. Inner control will scroll only if it Width is greater that ScrollViewer Width. Check that moment, please (ScrollViewer should not resize while increasing content size)
Just posted your code into blank page and it works fine
I have the following XAML :
<ScrollViewer HorizontalAlignment="Left" Margin="3,226,0,0" Name="Scv" VerticalAlignment="Top" ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollBarVisibility="Auto" >
<Canvas Height="100" Name="canvas1" Width="292" >
Image Canvas.Left="0" Canvas.Top="0" Height="440" Name="image1" Stretch="Fill" Width="730" / >
<InkPresenter Name="inkPresenter1"></InkPresenter>
</Canvas>
</ScrollViewer>
The problem : How do I stop the ScrollViewer from scrolling AFTER i have scrolled the image to the point I wanted.
In the Above example, I use scrollViewer to scroll the image to a section where I want to stop so that I can use InkPresenter to draw. But Whenever I draw Up or down, the scrollviewer follows the action of InkPresenter Up and down movement.
Thanks
Check this discussion:
How to block scrolling in ScrollViewer
Another option could be to dynamically set the SetVerticalScrollBarVisibility do Disabled:
ScrollViewer.SetVerticalScrollBarVisibility(scrollViewer, ScrollBarVisibility.Disabled);
Here is a simple XAML snippet:
<Grid x:Name="ContentGrid">
<ContentControl Background="Yellow" VerticalAlignment="Stretch"></ContentControl>
</Grid>
In WPF, the content control stretches the entire height of the grid.
In WP7 however, the content control does not stretch.
Why?
Avoid using ContentControl directly.
If you use something which inherits from it, such as a button, then, in a grid, as per your example, you'll automatically get horizontal and vertical stretching.
This will creat a button with a yellow background which fills the screen:
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<Button Background="Yellow" />
</Grid>
However, if you just wanted a yellow background to your grid - which is all your sample would do - you could just set the background property on the grid itself.
I fixed this by defining my own template:
<ContentControl><ContentControl.Template><ControlTemplate><ContentPresenter/> ...
Not sure why it fixes it but it works....