ZIndex and silverlight - silverlight

I have an image which constitutes the subject of a question and I want to place buttons on top of it for the user to click.
I have an object which positions a sprite (a rectangle with a background image which is transformed)
And I have an ItemsControl which positions the buttons using Margin.left and top. Like so
<Grid x:Name="imageGrid">
<local:spriteView Canvas.ZIndex="10" x:Name="QuestionImage" Sprite="{Binding QuestionImageSprite}" />
<local:ImageAnswers Canvas.ZIndex="50" x:Name="AnswersImages" Answers="{Binding answers}"></local:ImageAnswers>
</Grid>
I want AnswersImages to be positioned directly on top of QuestionImage but QuestionImage is pushing AnswerImages down and AnswerImages is also affecting the position of QuestionImage.
How do I stop them from doing that? Can I force them to be positioned absolutely within the ImageGrid

Remove the Canvas.ZIndex declarations. If your putting them both in a grid without specifying additional Column and Row definitions they will lay ontop of each other in the order they appear in the XAML

Related

How do I get a scrollbar to appear with a ListView when the height is auto?

I'm working on a Windows Store App.
I have a ListView with a bunch of items in it. It is nested in some stack panels and grids, but they all essentially are auto sized. Essentially, the ListView is given most of the right half of the screen, regardless of what size it is.
If I give the ListView a hard-valued Height, a scrollbar will automatically appear with no extra work. Great. But I don't want to set a height... I want it to be all of the area available in its container. If I try to be clever and set it to 9999 or something then it will not scroll.
I've done a bunch of research, and similar questions like this say that the thing that holds the ListView cannot give it infinite size like a StackPanel would. What alternatives do I have? How can I put a ListView in something with arbitrary space and get a scroll bar to appear?
My only thought is that there must be some way to, in the container that holds the ListView, tell the ListView that it has all available area in such a way that it's height gets set that that value. Sort of like how to have to set ListViewItem's HorizontalContentAlignment property to 'Stretch' in order to get the items within a ListView to know what what available width they actually have.
The essential bits of my layout is this:
<!-- Nested in some other stuff simple Grids and StackPanels, none of which has hard heights set (all auto or *) -->
<!-- Even if I made this a Grid with one Row, setting definition to * or Auto doesn't help the issue - no scroll bar appears -->
<StackPanel>
<!-- Other stuff that has Visibility="Collapsed"... I have code so that only one item at a time within this container will ever be visible, and it gets all available space. -->
<ListView ItemsSource="{Binding SomeBigList}" ItemTemplate="{StaticResource MyDataTemplate}" />
<!-- Other stuff that has Visibility="Collapsed" -->
</StackPanel>
How can I get a scrollbar to appear in the ListView without setting a hard height anywhere? Thanks for you're help.
Don't use a <StackPanel>.
If you are in control over what gets drawn and only one item is ever visible at any one time, then use a non-infinite sized container like <Grid>. You can put each item in the same row, or use a separate row if only for ease of spotting what's what.
I ran into similar on the Windows Phone app. You need to remove the variables of the other containers by creating a test page and determining which panel is causing your ultimate problem.
Create new blank page with a Grid and ListView.
Work the grid sizing to be automatic (auto) or * sized until you see the horizontal scroll bar.
Once the horizontal scroll bar appears, add the other containers, one by one until the scroll bar disappears. That will inform you of the culprit which is causing the miss-sizing and ultimately the failure of the scrollbar to appear.

How can I prevent WPF from clipping my image?

I am writing a WPF app that displays an image that is initially centered. The user can zoom in/out and move the image, which are implemented using ScaleTransform and TranslateTransform. That works great.
The problem is when the image is significantly bigger than the window, and the user moves the image or zooms out enough so that the entire image should be visible. The portion of the image that was originally hidden isn't rendered, and instead only the originally viewable part of the image is drawn.
Based on some other questions, if I put my image inside of a Canvas that will cause the entire image to be rendered, and when moved it will be rendered correctly. The problem is that I don't want my image to be in a Canvas, since that prevents any other layout from occurring - the HorizontalAlignment and VerticalAlignment properties are ignored (so the image is no longer centered), and I need to implement an option that will draw the image as large as possible to fill the entire area of the window which no longer works (setting the Stretch property to UniformToFill doesn't do anything).
Currently the two transforms are set to the RenderTransform property. If I use LayoutTransform instead, the entire image is drawn, but this also prevents the user from moving any portion of the image off the edge of the window (which is behavior that I would like to keep).
How can I tell WPF to always render the entire image without using a Canvas or a LayoutTransform?
I suggest not using a TranslateTransform but instead rely on the ScrollViewer.
<ScrollViewer>
<Grid>
<Image Source="blabla">
<Image.LayoutTransform>
<ScaleTransform />
</Image.LayoutTransform>
</Image>
</Grid>
</ScrollViewer>
The ScrollViewer gives the Image infinite space to expand, the entire image will be rendered. The Grid forces a centered layout while still allowing the image to expand. If you don't like the scrollbars to control the translate then you can hide them and roll your own solution.
LayoutTransform is definetely the way to go, so that the image's actual size in pixels (based on the zoom) is properly reflected onto your window.

How to create a UserControl with an irregular shape?

In my Silverlight 4 application I need to create a user control with an irregular shape. The "main display" of the UC is a standard rectangle but I need to have tabs (simple text blocks, where the user can click) that are outside of the main display rectangle.
Is this possible with Silverlight 4? If so, how?
Thanks in advance.
You can position elements of a control outside its normal layout in a number of ways. You could use Canvas but if most of the control is standard Grid rectangle then you can use a Grid. The trick is to use negative Margins.
<Grid x:Name="LayoutRoot">
<Border Margin="0 -22 0 0">
<TextBlock Text="I appear above the UserControl layout" />
</Border>
</Grid>
Note that if the Usercontrol is being used as the Visual root then this won't work because the Silverlight plugin will not render beyound its client rectangle.
It is, you can have transparent background behind the tabs which can let clicks through, effectively behaving as if the shape was different. The UserControl will still have a rectangular shape including the tabs, unless you wrap then into a Popup and float out of the UC with some offset.
Technically, you can have elements outside the UserControl's rectangle if you use a Canvas for your LayoutRoot instead of a Grid. Elements in a Canvas aren't clipped to the canvas size. I wouldn't recommend this, however, because you won't be able to use Margin to size and align your controls inside it. It would be better to have all child controls inside a Grid LayoutRoot.
Which brings us to the question of irregularity. If you want to 'see through' parts of the control and be able to click through them (i.e. click objects underneath it), all you need to do is keep the UserControl's and the LayoutRoot's Background to null or just not set it at all. Wherever there is a lack of any background, clicks will go through. Note that if you set the background to Transparent it will make the control behave as a rectangle (as if it's filled with solid color) with respect to mouse input.
Another thing is if you want to see HTML controls under the see-through parts of your app. Then, you'll have to use windowless mode, but that's another can of worms.

WPF/C#: Images rotating from a listview?

I just want ask for your comments/suggestions on how to create a customized listview (if that's a good implementation) in WPF that displays images coming from a table from a database (more like a playlist) that rotates similar to a film (moving horizontally - on loop)
Any ideas?
If you have a list of Images, you can create an Image control for each one, put each Image control in a horizontal StackPanel, put the StackPanel inside a Canvas (of whatever size of the "film"), and animate the Left property of the Canvas to have the images roll.
Of course, if you need that the images wrap (the first one after the last one), you could forget about the StackPanel and move each Image separately.

Stretching a WPF Canvas Horizontally

How do I make a Canvas stretch fully horizontally with variable width? This is the parent Canvas, so it has no parents, only children.
XAML Source: it displays in blend
http://resopollution.com/xaml.txt
Use a Grid as the top level element in your UI - it'll stretch to fill its container. Then put a Canvas with HorizontalAlignment="Stretch" inside the Grid and it'll behave the way you want.
<Grid xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Canvas Background="Blue"/>
</Grid>
That worked for me. The key is your top level UI element. While a Grid fills all available space by default, Canvases take up only as much room as their contents demand.
I'm guessing you've tried
canvas.HorizontalAlignment = HorizontalAlignment.Stretch
If this doesn't work, then what you could do is bind the Width and Height properties of the canvas to the ActualWidth and ActualHeight properties of the containing window.
You could use a dock panel to get it to fill the available width. The last item in a dock panel list of controls is automatically stretched to fill the remaining space.
<DockPanel>
<Canvas />
</DockPanel>
The canvas should do this automatically, unless you are manually setting the height and/or width. What kind of control are you trying to place the canvas on? Can you post your code?
The problem is that you're specifying the Height and Width. Without these properties, the control may appear to vanish in the designer, but it should size appropriately when you insert the canvas into another control.
If I recall correctly, the next version of WPF will have 'DesignWidth' and 'DesignHeight' properties that allow you to show the control in the designer with a given size without effecting it's measurement when inserted into other controls.

Resources