views:

2015

answers:

5

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

+1  A: 

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>
Lone Coder
Is there any quick way to do it for the <Canvas> tag?
resopollution
I tried removing the Height and Width property but everything just disappears.
resopollution
+1  A: 

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?

PeterAllenWebb
I've posted my code. The Canvas is the main parent element of the entire application.
resopollution
I tried removing the Height and Width property but everything just disappears.
resopollution
+1  A: 

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.

Robin
canvas.HorizontalAlignment = HorizontalAlignment.Stretch is done inside the C# code? I only have access to XAML.Also I don't have a containing Window either =(. Whenver I do the Binding Element, my silverlight app crashes.
resopollution
+5  A: 

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"&gt;
  <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.

James Cadd
Do you know if the "x:Class=" attribute is necessary? I tried using Grid as containment but for some reason everything just disappears.
resopollution
<?xml version="1.0" encoding="utf-8"?><Grid xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"><Canvas HorizontalAlignment="Stretch" VerticalAlignment="Stretch"> <Canvas.Resources>This did not work for me =(
resopollution
How are you testing? I pasted this into kaxaml and it works for me, so you don't need the x:class attribute:<Grid xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <Canvas Background="Blue"/></Grid>
James Cadd
I'll edit my code above - the paste here didn't work out.
James Cadd
Oh, and this shows the HorizontalAlignment=Stretch isn't necessary either.
James Cadd
Hmm, your example works if I just use your code, but when I try to wrap my entire code for the video player and preview in browser inside of a GRID tag, everything just disappears. I only see a white background. No XAML errors either.
resopollution
Could you post a full example of the Xaml that's not functional? The Xaml in the first post looks incomplete but if I could repro the error I might be able to help.
James Cadd
Hi James, I've linked to the source code here http://resopollution.com/xaml.txt
resopollution
First of all, nice player! The problem (which was a "duh" moment for me after re-reading your question) is that the root element is a canvas. I took the code you posted and set the parent canvas background to blue for testing. Then, I wrapped that canvas in a parent grid and removed its width and height properties. At that point I saw a small black media player at the top left of the screen and the rest of the screen filled with blue. Canvases don't stretch to fill the parent automatically *unless* you place one in a Grid. By default they're only the size of their inner content. Hth!
James Cadd
So to get the video to display at the bigger size, I'd have to put every single canvas inside a <grid>?
resopollution
I think I found the solution, in addition to what you said, I added a VerticalAlignment="Top" and HorizontalAlignment="Left" to the canvas inside of the outer grid and everything magically worked. Totally random but your comments helped guide the way :D
resopollution
Glad you got it working ;)
James Cadd
Whew! Thanks for that trick.
John Robertson
+1  A: 

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.

YotaXP