Partager via


Content.ActualWidth Property

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Gets the browser-determined width of the Silverlight plug-in content area.

Namespace:  System.Windows.Interop
Assembly:  System.Windows (in System.Windows.dll)

Syntax

'Declaration
Public ReadOnly Property ActualWidth As Double
public double ActualWidth { get; }

Property Value

Type: System.Double
The browser-determined width, in pixels, of the Silverlight plug-in content area. The default value is the width of the Silverlight plug-in, as specified by the HTML object element that instantiated it.

Remarks

Important noteImportant Note:

In the early stages of the object lifetime of a Silverlight plug-in instance, ActualHeight and ActualWidth do not contain usable values. In particular, the plug-in DOM-level OnLoad event does not yet guarantee a correct value for ActualHeight and ActualWidth. In general, you should check these values in the handler for the Resized event, which occurs just after OnLoad.

The primary scenario where ActualWidth might be unknown, even to the caller that instantiated the plug-in, is when the initial width value is specified as a percentage. In this case the browser must calculate a pixel width value, and ActualWidth returns that value. Also, a Silverlight plug-in can display in either embedded mode or full-screen mode:

  • In embeddedmode, the plug-in displays in the Web browser window.

  • In full-screen mode, the plug-in resizes to the current resolution of the screen and displays on top of all other applications.

When the Silverlight plug-in is in embedded mode, the value of ActualWidth depends on whether the width of the Silverlight plug-in is a fixed or percentage value. The width of the Silverlight plug-in is defined in the OBJECT tag of the containing Web page. If the width of the Silverlight plug-in is a fixed value, such as "300", ActualWidth is set to the plug-in width. If the width of the Silverlight plug-in is a percentage value, such as "100%", ActualWidth is set to the most appropriate width for displaying the plug-in as determined by the browser. In this case, resizing the browser window size causes the ActualWidth value to change.

When the application is in full-screen mode, the value of ActualWidth is set to the current horizontal resolution of the screen.

The Resized event occurs whenever the ActualHeight and ActualWidth properties change, and the Silverlight plug-in is in embedded mode. When the Silverlight plug-in is in full-screen mode, the Resized event does not occur. However, when full-screen mode is first entered, the FullScreenChanged event is raised.

Examples

The following code example demonstrates how to use this property.

Private WithEvents rootPage As Page = New Page()
Private WithEvents htmlContent As Content
Private Sub Application_Startup(ByVal o As Object, ByVal e As StartupEventArgs) Handles Me.Startup
    Me.RootVisual = rootPage
    htmlContent = Me.Host.Content
End Sub

Private Sub ToggleFullScreen(ByVal sender As Object, _
    ByVal e As MouseButtonEventArgs) Handles rootPage.MouseLeftButtonDown
    Me.Host.Content.IsFullScreen = Not Me.Host.Content.IsFullScreen
End Sub

Private Sub DisplaySizeInformation( _
    ByVal sender As Object, ByVal e As EventArgs) _
    Handles htmlContent.FullScreenChanged, htmlContent.Resized

    Dim message As String = String.Format( _
        "ActualWidth={0}, ActualHeight={1}", _
        Me.Host.Content.ActualWidth, _
        Me.Host.Content.ActualHeight)

    rootPage.LayoutRoot.Children.Clear()
    Dim t As New TextBlock()
    t.Text = message
    rootPage.LayoutRoot.Children.Add(t)

End Sub
Page rootPage = new Page();
private void Application_Startup(object sender, StartupEventArgs e)
{
    this.RootVisual = rootPage;

    rootPage.LayoutRoot.MouseLeftButtonDown +=
        delegate(Object s, MouseButtonEventArgs args) {
            this.Host.Content.IsFullScreen =
                !this.Host.Content.IsFullScreen;
        };

    this.Host.Content.FullScreenChanged += 
        new EventHandler(DisplaySizeInformation);

    this.Host.Content.Resized += 
        new EventHandler(DisplaySizeInformation);
}

private void DisplaySizeInformation(Object sender, EventArgs e)
{
    String message = String.Format(
        "ActualWidth={0}, ActualHeight={1}",
        this.Host.Content.ActualWidth,
        this.Host.Content.ActualHeight);

    rootPage.LayoutRoot.Children.Clear();
    rootPage.LayoutRoot.Children.Add(
        new TextBlock { Text = message });
}

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.