I'm trying to implement zoom-functionality in a RadSlideView ItemTemplate. I'm doing this by using a ViewportControl with a Canvas and then applying a RenderTransform (ScaleTransform) to a StackPanel in the Canvas. Similar to the SDK-sample found here.
The problem I have is that the ScaleTransform seems to be affecting the swipe-gesture used to change item in the SlideView/Panorama/Pivot control. E.g. if the ScaleTransform is set to 0.1 it seems like I only need to swipe 1/10th of the length to change item compared to using a ScaleTransform of 1.0.
I found that if I set IsHitTestVisible to false on the ItemTemplate the swiping works like I want. But this is not a solution since I sometimes need to be able to pan the content vertically while still being able to change item by swiping horizontally.
So my question is how can I solve this?
For reference the XAML looks like this:
<Controls:RadSlideView Name="SlideView" ItemsSource="{Binding Pages}" IsLoopingEnabled="False" SelectionChanged="RadSlideView_SelectionChanged" CacheMode="BitmapCache" ManipulationStarted="SlideView_ManipulationStarted" ManipulationCompleted="SlideView_ManipulationCompleted" ManipulationDelta="SlideView_ManipulationDelta">
<Controls:RadSlideView.ItemTemplate>
<DataTemplate>
<ViewportControl x:Name="SlideViewViewport" ViewportChanged="SlideViewViewport_ViewportChanged" Loaded="SlideViewViewport_Loaded">
<Canvas>
<StackPanel>
<Image Source="{Binding Image}" Stretch="Fill" Width="{Binding ElementName=SlideView, Path=DataContext.PageWidth}" Height="{Binding ElementName=SlideView, Path=DataContext.PageHeight}" CacheMode="BitmapCache"/>
<StackPanel.RenderTransform>
<ScaleTransform x:Name="xform"/>
</StackPanel.RenderTransform>
</StackPanel>
</Canvas>
</ViewportControl>
</DataTemplate>
</Controls:RadSlideView.ItemTemplate>
I have also looked at Teleriks RadPanAndZoom-control to avoid implementing my own zoom-functionality, but since I sometimes need to place two pictures side by side and zoom them as if they were one I don't think I can use it.
The problem is that ScaleTransformation scales your picture, but doesn't change it's height and width. Only if Height And Width are overflowing scrollviewer you can scroll the content
Related
I have a listbox with Items that all have a random background color.
In each Item of the listbox i want to display a Bitmap picture.
Now for some reason the background of each bitmap (which I've set to Color.Transparent) Shows up black.
Here a picture of how it looks
My code for the listbox:
<WrapPanel Grid.Row="1" Grid.Column="1" Margin="6" >
<ListBox x:Name="CharListBox" BorderThickness="0" ScrollViewer.VerticalScrollBarVisibility="Hidden"
ItemsSource="{Binding ListToDisplay, UpdateSourceTrigger=PropertyChanged}"
ItemTemplate="{DynamicResource ItemTemplate1}"
SelectionChanged="SelectionChangedNewCharSelected">
</ListBox>
</WrapPanel>
The item template (I removed everything beside the image part for bettter overview):
<DataTemplate x:Key="ItemTemplate1" >
<Image Source="{Binding OutfitImageSource, UpdateSourceTrigger=PropertyChanged}" Height="40" Width="40" />
</DataTemplate>
The Binding binds to a BitmapSource.
Is it possible that bitmaps dont have actual transparency, its just shown as black?
I also tried to add AllowsTransparency="True" to the window properties, this just lead to the window crashing instantly..
Thank you for helping in advance!
update: AllowsTransparency works if i set WindowStyle="None" but it still does not fix the problem with the untransparent bitmaps.
As reported in this answer
Bitmaps (i.e. files with .BMP extension) do not natively support transparency: you need to save as a different format like PNG.
You can find the same info also here.
So it does not depend on your XAML or your code. Just save your bitmaps as PNG files and then use those files for your application.
Have a checkbox with custom control template which looks like this in design view:
XAML code:
<CheckBox IsChecked="{Binding Fareklasse21}" VerticalAlignment="Center" HorizontalAlignment="Center" Grid.Row="1" Grid.Column="1">
<CheckBox.Template>
<ControlTemplate>
<StackPanel Orientation="Vertical" VerticalAlignment="Center">
<Image Source="pack://application:,,,/Asd.WWs.Client.Wpf;component/Resources/ADR-M.png" Width="64" Height="64" SnapsToDevicePixels="True">
</Image>
<StackPanel Orientation="Horizontal">
<CheckBox IsChecked="{Binding IsChecked, Mode=TwoWay, RelativeSource={RelativeSource AncestorType={x:Type CheckBox}}}"></CheckBox>
<TextBlock>2.1</TextBlock>
</StackPanel>
</StackPanel>
</ControlTemplate>
</CheckBox.Template>
When I start the application, the image (which is originally 64x64px) gets distorted (and enlarged?)
Could it be that the image inherits some value from the Prism wrapper? I can't really see anything interesting while doing live inspection:
Here are the properties of the specific image:
The WPF graphics system uses device-independent units to enable resolution and device independence. Each device independent pixel automatically scales with the system's dots per inch (dpi) setting. This provides WPF applications proper scaling for different dpi settings and makes the application automatically dpi-aware. See also wiki page.
This is the reason why if you even specify Stretch=None for an Image control, you may notice that the image does not appear at the expected number of pixels wide/high. This can happen if the image’s resolution (DPI) doesn’t match your current system DPI.
The conversion from physical pixels to DIPs uses the following formula.
DIPs = pixels / (SystemDPI / 96.0)
If you want to convert DIP to the "real" pixels you can use next formula:
Width (pixels) = Width (WPF Units) * (SystemDPI / 96)
Height (pixels) = Height (WPF Units) * (SystemDPI / 96)
You can specify element size in DIP, inches, centimeters or points. But it's better to use vectorized graphics if possible.
If you have SVG files you can use sharpvector framework via nuget:
Install-Package SharpVectors
So there is SvgViewbox to render SVG in XAML:
<UserControl ...
xmlns:svgc="http://sharpvectors.codeplex.com/svgc/"
...>
...
<svgc:SvgViewbox Margin="5" Height="20" Width="20" Stretch="Uniform" Source="/Resources/Icons/Sample.svg"/>
...
</UserControl>
I would like to paint Images to the XAML page in Windows Store App.
The main goal is that:
Adding Images (e.g. flower leaf) to a circle on the center like that:
I have a simple solution to that, but its is very redundant.
<Image Height="200" Width="200" Source="{Binding ActualImage.NormalUri}">
<Image.RenderTransform>
<RotateTransform Angle="12"></RotateTransform>
</Image.RenderTransform>
</Image>
... // And 28 other like these
<Image Height="200" Width="200" Source="{Binding ActualImage.NormalUri}">
<Image.RenderTransform>
<RotateTransform Angle="360"></RotateTransform>
</Image.RenderTransform>
</Image>
How can I do that with a binding of a Image collection? What XAML control should I use?
Use a custom class which inherits from ItemsControl. You can then override the necessary functions, such as ones to determine the angle to rotate between each item. I think it's likely you'll want to use this PrepareContainerForItemOverride for this.
One thing to note that you will have to do is to define a new ItemsPanel. The default is a StackPanel, which will not work. You'll likely want to use something like a Canvas, which allows you to explicitly position items in it.
The Solution of Nate Diamond is much more nicer and better, but I solved it from code-behind for earn the easier way:
foreach (Petal petalObject in MainPageViewModel.Petals)
{
var petalImage = new Image
{
Height = petalObject.Height,
Width = petalObject.Width,
RenderTransform = new RotateTransform() {Angle = petalObject.Angle},
Source = new BitmapImage(new Uri(petalObject.NormalUri)),
};
PetalsGrid.Children.Add(petalImage);
}
Is there any way to exclude certain UIElement from a certain scroll effect? I have a grid element below a columns header element. while I'd like to have full scrolling over the grid, I want the header to be affected only by the horizontal scrolling. so that it will always stay at the top when scrolling down.
Here is a mspaint I made to explain in case my explenation didn't make sense.
Why not move your scroll viewer inside the green rows and then have a different scroll viewer for the header.
Then wrap it all in a parent container
So like
<Grid>
<HeaderControl>
<ScrollViewer />
</HeaderControl>
<BodyControl>
<ScrollViewer />
</BodyContent>
</Grid>
Wrapping them together in one scroll viewer is overly complicated. Splitting it into two distinct scroll viewers will simplify the problem IMO.
What if you used 2 separate scroll viewers - an outer one that contains both the header and the grid, and that only handles the horizontal scrolling.
Then the grid itself can be wrapped in a separate scroll viewer that handles just the vertical scrolling on of the grid. E.g:
<ScrollViewer VerticalScrollBarVisibility="Disabled">
<!-- Header -->
<Grid>
</Grid>
<ScrollViewer HorizontalScrollBarVisibility="Hidden">
<!-- Content -->
<Grid>
</Grid>
<ScrollViewer />
</Grid>
Although I'm not 100% sure about the two visibility settings - you might have to play around with them to see what works
I just wanted to do the same. My solution would be using a Canvas for the header and Scrollviewer for the body.
<Canvas>
<Grid Canvas.Left="{Binding ElementName=ValueScrollViewer, Path=HorizontalOffset, Converter={StaticResource DoubleMultiplyingConverter}, ConverterParameter=-1}">
Header
</Grid>
</Canvas>
<ScrollViewer Name="ValueScrollViewer" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" >
<Grid>
Table
</Grid>
</ScrollViewer>
With converter:
public class DoubleMultiplyingConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var factor = System.Convert.ToDouble(parameter, CultureInfo.InvariantCulture);
var val = System.Convert.ToDouble(value);
return val * factor;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
To handle all the situations, you should separate static part (header) and scrollable part (body) and wrap body with ScrollViewer. This solution is perfect in everything, except the situation when your control is wider than screen (or outer control), so horizontal scroll should be allowed too and header should be also scrolled. You can't just use inner scrollviwer for this, because vertical scrollbar will only be visible when horizontal scroll is at right edge.
You can't exclude header from only vertical scroll, but you can simulate horizontal one. You can do it with just translation transform.
<Grid>
<!-- body part. place it first to make it go to the background -->
<ScrollViewer HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Visible" x:Name="scrollbar">
<!-- the body control itself -->
<Canvas x:Name="PART_Body"/>
</ScrollViewer>
<!-- header part. Border and margin are used to make it not overlap vertical scrollbar -->
<Border Margin="0,0,18,18" ClipToBounds="True">
<Canvas x:Name="PART_Header">
<Canvas.RenderTransform>
<!-- bind X to the scrollbar X position -->
<TranslateTransform X="{Binding ElementName=scrollbar, Path=ContentHorizontalOffset, Converter={StaticResource InvertDoubleConverter}}"/>
<!-- InvertDoubleConverter is just a value converter which multiples incmoing double value by -1 -->
</Canvas.RenderTransform>
</Canvas>
</Border>
</Grid>
I have created a Rectangle inside of a ScrollViewer like this
<ScrollViewer ManipulationMode="Control" x:Name="songScrollViewer" HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Disabled" Height="270" VerticalAlignment="Center" Width="728" Canvas.Top="20" d:LayoutOverrides="HorizontalMargin" >
<Rectangle x:Name="musicBG" Fill="#FF0692FD"/>
</ScrollViewer>
During the use of the app, the size of MusicBg changes, sometimes to something around 3,000 pixels width.
musicBG.Width = _songLength*PixelsPerSecond
However, while scrolling the scrollViewer, it allows me to scroll the rectangle all the way off the screen.
For example this line of code gives me the following values when I have moved the rectangle as far as I want to move it.
if (songScrollViewer.HorizontalOffset > songScrollViewer.ScrollableWidth)
HorizontalOffset has a value of ~1200 and ScrollableWidth has a value of about ~2900.
How can I get this to be done properly so that the rectangle is not scrolled completely off the screen?
I would expect a HorizontalOffset of about 1200 to only push the rectangle about halfway through to it's destination, and not make it start going off screen.
ANSWER:
After much frustration, I was able to solve this problem by using Canvas instead of Border or Rectangle.
I'll award points if anyone can explain why this problem happened, and if there is a less processor intensive control that would work better than canvas.
Edit: Screen shots:
Bad Code:
<ScrollViewer ManipulationMode="Control" x:Name="songScrollViewer" Width="720" HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Disabled" Height="270" VerticalAlignment="Top" Canvas.Top="20" HorizontalAlignment="Left" >
<Border x:Name="musicBG" Background="#FF0692FD" VerticalAlignment="Top" HorizontalAlignment="Left" Height="270" />
</ScrollViewer>
Image of bad scroll with bad code:
Good working code:
<ScrollViewer ManipulationMode="Control" x:Name="songScrollViewer" Width="720" HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Disabled" Height="270" VerticalAlignment="Top" Canvas.Top="20" HorizontalAlignment="Left" >
<Canvas x:Name="musicBG" Background ="#FF0692FD" Height="270" >
<Border Background="#FF0692FD" VerticalAlignment="Top" HorizontalAlignment="Left" Height="270" />
</Canvas>
</ScrollViewer>
Good Scroll: Notice it says 170 seconds on the bottom right instead of the smaller number of 118 seconds in the bad scroll.
I believe your right, wp7 won't render shapes that are bigger then 2048 pixels. So the reason it's scrolling of the page is because it's treating it as if it were bigger then 2048 but you can only see up to a width of 2048px and its just scrolling over to the "ghost" part of the rectangle.
I'm not sure if you can override this but the best solution I could come up with (without overriding) is by splitting up your rectangle into chucks that are smaller then 2000 (just to be safe) and then displaying them seamlessly in a horizontal stack panel inside the scroll viewer. The problem with this is that depending on how you've coded it, this solution might be hard to implement; but you might just be able to split it in your ViewModel when displaying it and your logic would only see it as one big chunk.