Filling Browser Window with Silverlight Application - silverlight

I want my Silverlight app to fill the entire browser window. I've set the plugin object width and height to 100%, and set my LayoutRoot container's height and width to Auto, but still no luck. Any suggestions?
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<asp:Silverlight ID="Silverlight1" runat="server"
Source="~/ClientBin/Client.xap"
MinimumVersion="2.0.30818.0"
AutoUpgrade="true"
Height="100%"
Width="100%">
</asp:Silverlight>
</form>
<UserControl
x:Class="Client.Page"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="Auto"
Width="Auto">
<Grid
x:Name="LayoutRoot"
Background="#084E85"
ShowGridLines="True">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="280" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="80" />
<RowDefinition Height="Auto" />
<RowDefinition Height="600" />
</Grid.RowDefinitions>
...Remaining content here...
</Grid>
</UserControl>
Disclaimer: I searched for an answer first, finding this thread. However, as you can see by my code that isn't working for me.

First, I don't set the height/width in the user control. Instead, I set the DesignHeight and DesignWidth (in the "http://schemas.microsoft.com/expression/blend/2008" namespace) and I set the alignment to stretch
<UserControl ...
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
d:DesignHeight="1050" d:DesignWidth="1680">
In my HTML, I set the Height and Width to 100% like this...
<div style="height: 100%; width: 100%; position: fixed;">
<asp:Silverlight runat="server" Source="~/ClientBin/My.xap" ID="MyId"
Width="100%" Height="100%" />
</div>
At that point, everything works for me to have it take the entire window.

Remove the Height and Width attributes from the :
<UserControl
x:Class="Client.Page"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
Auto Height and Width with resize the window to fit the content. If you remove the Height and Width attributes, your Silverlight Application will grow to fit the window.

Hmm
I'm thinking your Grid is set-up to be as small as possible (no "star" column or row).
Can you try with one more column and row with a "star" size ?
as other have pointed out, you also need to remove the "auto" sizes, since they auto-size to content.
also try to set a background color on the page, so you see where it actually extends.

If you've got a fairly complex HTML layout and can't rely on fixed positioning then you can make the #silverlightControlHost div resize using the following:
private bool _hasResized;
protected override Size ArrangeOverride(Size finalSize)
{
if (!_hasResized)
{
HtmlPage.Document.GetElementById("silverlightControlHost").SetStyleAttribute("height", finalSize.Height.ToString());
_hasResized = true;
}
return base.ArrangeOverride(finalSize);
}
You can put this inside MainPage.cs or if you have nested UserControls, then the control that requires the most height. I'm also using the following XAML and the default HTML Visual Studio provides
<UserControl ...
VerticalAlignment="Stretch"
Height="Auto"
Width="Auto">
I haven't tested it without these settings, as far as I know Auto is the default.

Related

Control anchoring in Silverlight

This may be a bit of a silly question, but I'm not sure what else to try, so wanted to ask for some help. My problem is with anchoring controls in Silverlight.
I've got a grid (C1FlexGrid) which sits in a control. The control is used in a page and I simply want the grid size to be determined by the size of the browser window. I want to have a minimum size for it, but allow it to extend both vertically and horizontally as the user expands the browser window. In WinForms (which is what I'm used to) this is easy.
However, in Silverlight it's causing me a headache. Is it possible to get it to behave the way I want?
I've set the control width and height to auto, and specified some design width/height. The user control is then placed onto a page and both the control and page width/height set to auto.
When the grid loads its data, a few hundred rows, it automatically sizes the grid based on the fact in has 200 rows, i.e. becomes very long.
EDIT
Here's some XAML from a simple example:
<navigation:Page
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
xmlns:c1="http://schemas.componentone.com/winfx/2006/xaml" x:Class="Optimize.Client.Presentation.AboutView"
mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480"
Title="About"
x:Name="AboutPage"
Style="{StaticResource PageStyle}">
<Grid x:Name="LayoutRoot" Background="White" MinWidth="300" MinHeight="300">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<c1:C1FlexGrid Margin="10" BorderBrush="Red" BorderThickness="1" Width="Auto" Height="Auto" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch">
<c1:C1FlexGrid.Columns>
<c1:Column Header="User Group" Width="*" />
</c1:C1FlexGrid.Columns>
</c1:C1FlexGrid>
</Grid>
</navigation:Page>
All I want here is a grid on a page. The page has a minimum size of 300x300. When the browser window expands I want the page to expand with it, and the grid to also expand with it, so that all borders of the grid are 10 from top/bottom/left/right. I've tried specifying * for the width/height of the layout grid, but it's still not working.
Thanks
Auto sizing means "please resize me to my content". That will not limit you to the size of the parent control or browser.
You want Star (*) sizing which basically means "after deducting any fixed or auto columns/rows, please distribute the remaining space among any star-sized columns/rows based on the ratio of star sizes".
You also want your containers to specify HorizontalAlignment="Stretch" and VerticalAlignment="Stretch" so that the amount of space distributed to the star-sized columns/rows can be determined (otherwise it will collapse on itself).
I can be more specific if you can post your XAML.

Window Resize with Mouse drag?

I have a WPF Application.I want to implement Resize the window(whatever size) using mouse drag with window corners or sides. like notepad or wordpad.Controls are strecthed according to window size.
can any one help on this?
By default, the Window class (<Window> element) comes with style that is 'Resizeable' - that is a thik border which the user can resize by dragging. If you do nothing to the Visual Studio's generated WPF Window item you'll get a resizeable Window: The property that dictates this behavior is ResizeMode. If you want to have a little Grip, then set the followign property: ResizeMode="CanResizeWithGrip"
There are many ways to acheive controls proportional to the embedding element. One common method is to use the element, and define ahd columns width and row heights in terms of '*'. The example below will drow two buttons that are 2/5 of the height each:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="2*" />
<RowDefinition Height="2*" />
<RowDefinition Height="1*" />
</Grid.RowDefinitions>
<Button Grid.Row="0" />
<Button Grid.Row="1" />
</Grid>

Putting Canvas Inside Viewbox in Silverlight With Image

All,
I'm trying to make the silverlight app I'm making resize in a reasonable manner. To do this, I thought I would use a dynamic grid. In the center of the grid I need to put an image inside of a canvas because I will dynamically be positioning objects on top of it. Ideally, as the user makes the browser window larger, the center column would be able to resize and grow larger, thereby growing the image.
Here's what I've got:
<Viewbox Grid.Row="0" Grid.Column="1">
<Canvas x:Name="cvsCenterPane">
<Image x:Name="imgFormImage" MouseLeftButtonDown="imgFormImage_MouseLeftButtonDown"
MouseLeftButtonUp="imgFormImage_MouseLeftButtonUp" MouseMove="imgFormImage_MouseMove" />
</Canvas>
</Viewbox>
In the code behind, I then set the image source.
Here's my grid definition:
<Grid x:Name="LayoutRoot" Background="DarkCyan" ShowGridLines="True">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="300" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="300" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="100" />
</Grid.RowDefinitions>
I think there must be some sort of unhandled exception occurring during the construction of the Viewbox because the image does not display at all. What am I doing wrong here? Am I taking the wrong approach?
There is no exception, the canvas just does not have any set dimensions which is a requirement when you want to use a ViewBox.
I for one would not use a canvas, you can stick with the grid since you can place more than one control in a cell and if you need to move the objects around you can use the Margin or a TranslateTransform in the RenderTransform property.
This does not work because you cannot set the x:Name attribute of children in a Viewbox with silverlight. At least according to a few sources:
http://blog.ningzhang.org/2008/11/viewbox-control-in-silverlight-toolkit.html
http://forums.silverlight.net/forums/p/48535/128830.aspx
http://forums.silverlight.net/forums/p/45789/123941.aspx#123941
this seems to be by design. A few workarounds have been suggested, so I will try them.
Edit: HB seems to be correct, there is no exception, the Canvas needed to have dimensions set.

UserControl Dimensions Ignored in MainPage Layout

I have a Canvas with a Grid on it. The Grid contains 3 columns. The first column contains another Grid, the second contains nothing, the third contains an Image (a box). (I have the blank column for animation purposes unrelated to this question.)
In my child Grid I have 2 rows. The first row contains a UserControl (shelves of boxes), the second contains an Image (a barcode).
The problem is that the layout engine seems to completely ignore the dimensions of the user control. In the preview I see my UserControl and directly on top of it the two other images. Granted Canvas doesn't automatically size its children, but I have that resolved with some resize statements in the SizeChanged event of the page.
As it is, the box shows up at runtime in the bottom right corner of my window as it should, however the barcode appears positioned as if the UserControl doesn't exist.
For an illustration of my issue please see http://www.wynright.com/temp/Problem.png
The following is my XAML code:
<Canvas x:Name="MainCanvas" Background="Silver">
<Grid Name="LayoutRoot">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Grid Name="LayoutChild" Grid.Column="0">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<LightPickDemo:Picker x:Name="PickZone" ZoneCleared="PickZone_ZoneCleared" HorizontalAlignment="Left" VerticalAlignment="Top" />
<Image Name="BarCode" Source="/LightPickDemo;component/Images/BarcodeScan.png" HorizontalAlignment="Center" Stretch="None" VerticalAlignment="Center" Grid.Row="1" />
</Grid>
<Image Name="imgShippingBox" Source="/LightPickDemo;component/Images/Empty-Box.png" VerticalAlignment="Bottom" Stretch="None" Grid.Column="2" />
</Grid>
</Canvas>
So I wonder what do I need to do to get the UserControl to offer its size for the layout engine. I can see in my designer the size of the control, so clearly the compiler knows that there is size to this control.
I would like the barcode to show centered below the UserControl.
Here is the XAML for the UserControl:
<Canvas>
<Image Name="ImageBase" Source="/LightPickDemo;component/Images/BayShelves.png" />
<LightPickDemo:ZoneLight x:Name="ZoneLight" Canvas.Left="345" Canvas.Top="0" ButtonPress="ZoneLight_ButtonPress" />
<LightPickDemo:PickerModule x:Name="pickerCalculator" Canvas.Left="74" Canvas.Top="266" OnPicked="Picker_PickChanged" />
<LightPickDemo:PickerModule x:Name="pickerCalendar" Canvas.Left="207" Canvas.Top="266" OnPicked="Picker_PickChanged" />
<LightPickDemo:PickerModule x:Name="pickerPaperClip" Canvas.Left="521" Canvas.Top="266" OnPicked="Picker_PickChanged" />
<LightPickDemo:PickerModule x:Name="pickerRuler" Canvas.Left="649" Canvas.Top="266" OnPicked="Picker_PickChanged" />
<LightPickDemo:PickerModule x:Name="pickerScissor" Canvas.Left="75" Canvas.Top="532" OnPicked="Picker_PickChanged" />
<LightPickDemo:PickerModule x:Name="pickerStapler" Canvas.Left="250" Canvas.Top="532" OnPicked="Picker_PickChanged" />
<LightPickDemo:PickerModule x:Name="pickerStapleRemover" Canvas.Left="435" Canvas.Top="532" OnPicked="Picker_PickChanged" />
<LightPickDemo:PickerModule x:Name="pickerTapeDispenser" Canvas.Left="635" Canvas.Top="532" OnPicked="Picker_PickChanged" />
</Canvas>
Most likely the ActualHeight of your user control is zero. Looking at your XAML for the user control you should be able to just set Height and Width in the XAML of the user control.
I can't determine what is the UserControl from your image, but if you set the property to 'collapsed' the engine won't read the size I don't think. If the UserControl isn't visible, but you want to show the dimensions, animate the opacity property instead of visibility. The barcode is in Row 1 which is the top row, should it be in row 2?
Am I correct in assuming the barcode needs to be below the image with the 2 rows of boxes? Where is the UserControl? What is it?
An answer to my question seems to be to put the following code in my UserControl, however, I can't imagine this is the best answer and I know it doesn't allow for screen scaling at all. So please, if anyone knows how to do this correctly, please let me know.
Thanks to Wallstreet Programmer for leading me to this workaround.
Protected Overrides Function MeasureOverride(ByVal availableSize As System.Windows.Size) As System.Windows.Size
Dim bmp As BitmapImage
bmp = BaseImage.Source
Return New Size(bmp.PixelWidth, bmp.PixelHeight)
'Return MyBase.MeasureOverride(availableSize)
End Function

Sizing the content to fit into screen resolution

hello i have a window(wpf) with labels and text boxes, i want him to fit to the screen resolution as much as possible,
how do i do it
Viewbox is quite useful if you need the content of your window to scale proportionally when you resize the window (for example maximize it). In this minimalistic page
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Viewbox>
<StackPanel>
<TextBlock FontSize="14">Example</TextBlock>
<Border Background="Aqua" Width="100" Height="100"></Border>
</StackPanel>
</Viewbox>
</Window>
you have a TextBlock and a colored Border stacked vertically; if you start this xaml the window will have a size of 300x300, the font of the TextBlock will be 14 in size and the colored border will be 100x100. If you rescale the window you will see both the TextBlock and the Border scale accordingly (so they'll be no more of the size you've specified in the xaml), keeping relative proportions. Viewbox is really useful, in this respect, if you need a window whose internal components layout look always the same independently from the final resolution it will be displayed (what does matter is aspect-ratio, thought). This obviously work with any contents you'll put inside the Viewbox (we had an application with videos and 3D views for example). Note that in Visual Studio 2008 you'll not be able to see the content of the Viewbox in the Designer.
Hope this help.
If you want to scale really everything including font sizes, you could probably apply a scale transform to your content, and bind it's X and Y values to the window's width and height. You would then also need a value converter to convert those to the appropriate scale.
If you want to scale everything to the size of the window just put everything inside a Viewbox control.
Do you mean you want the window to fill the entire screen? The simplest way to do that (without causing further headaches) is to maximise the window.
w.WindowState = WindowState.Maximized;
EDIT:
A scalable window layout requires you to avoid using the XAML editor in Visual Studio! Actually you can do it in the editor, but it is very hard.
Much easier to write the XAML by hand:
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Label Grid.Column="0" Grid.Row="0">First Name</Label>
<TextBox Grid.Column="1" Grid.Row="0" Name="firstName">Fred</TextBox>
<Label Grid.Column="0" Grid.Row="1">First Name</Label>
<TextBox Grid.Column="1" Grid.Row="1" Name="lastName">Smith</TextBox>
</Grid>
This will size to fit the window, though may look strange, as the rows and columns will by default get half the space each. You can override this so they have a height determined by their contents instead:
<RowDefinition Height="Auto"/>
It can also help to put margins on some controls, to space them out:
<TextBox Grid.Column="1" Grid.Row="1" Margin="6" Name="lastName">Smith</TextBox>
Add WindowState="Maximized" to the Window
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300" WindowState="Maximized" >
</Window>

Resources