In This Article

WindowControl Class

Represents a ContentControl that renders like a normal native Window but can be used in other non-top-level contexts.

[TemplateVisualState(Name = "Active", GroupName = "ActiveStates")]
[TemplateVisualState(Name = "Inactive", GroupName = "ActiveStates")]
[TemplateVisualState(Name = "FrameHidden", GroupName = "FrameStates")]
[TemplateVisualState(Name = "FrameVisible", GroupName = "FrameStates")]
[TemplateVisualState(Name = "Editable", GroupName = "ReadOnlyStates")]
[TemplateVisualState(Name = "ReadOnly", GroupName = "ReadOnlyStates")]
[TemplateVisualState(Name = "Maximized", GroupName = "WindowStates")]
[TemplateVisualState(Name = "Minimized", GroupName = "WindowStates")]
[TemplateVisualState(Name = "Restored", GroupName = "WindowStates")]
[TemplatePart(Name = "PART_Icon", Type = typeof(FrameworkElement))]
[TemplatePart(Name = "PART_TitleBar", Type = typeof(FrameworkElement))]
public class WindowControl : ContentControl
Inheritance:
object Visual UIElement FrameworkElement Control ContentControl object
Derived:
FloatingWindowControl StandardMdiWindowControl

Constructors

WindowControl()

Initializes an instance of the class.

public WindowControl()

Properties

CanClose

Indicates whether the window can close.

public bool CanClose { get; set; }

Property Value

bool:

The default value is true.

CloseCommand

The ICommand that can close the window.

public ICommand CloseCommand { get; }

Property Value

ICommand

ContextContent

The context content that is displayed on the title bar next to the title.

public object? ContextContent { get; set; }

Property Value

object:

The default value is null.

ContextContentTemplate

The DataTemplate to use for the ContextContent.

public DataTemplate? ContextContentTemplate { get; set; }

Property Value

DataTemplate:

The default value is null.

ContextContentTemplateSelector

public DataTemplateSelector? ContextContentTemplateSelector { get; set; }

Property Value

DataTemplateSelector:

The default value is null.

CornerRadius

The CornerRadius of the window.

public CornerRadius CornerRadius { get; set; }

Property Value

CornerRadius:

The default value is 0.0.

HasCloseButton

Indicates whether the window is capable of displaying a close button.

public bool HasCloseButton { get; set; }

Property Value

bool:

The default value is true.

HasDropShadow

Indicates whether the window has a drop shadow.

public bool HasDropShadow { get; set; }

Property Value

bool:

The default value is false.

HasIcon

Indicates whether the window is capable of displaying an icon.

public bool HasIcon { get; set; }

Property Value

bool:

The default value is true.

HasMaximizeButton

Indicates whether the window is capable of displaying a maximize button.

public bool HasMaximizeButton { get; set; }

Property Value

bool:

The default value is true.

HasMinimizeButton

Indicates whether the window is capable of displaying a minimize button.

public bool HasMinimizeButton { get; set; }

Property Value

bool:

The default value is true.

HasRestoreButton

Indicates whether the window is capable of displaying a restore button.

public bool HasRestoreButton { get; set; }

Property Value

bool:

The default value is true.

HasTitleBar

Indicates whether the window is capable of displaying a title bar.

public bool HasTitleBar { get; set; }

Property Value

bool:

The default value is true.

Icon

The ImageSource displayed as the window icon.

public ImageSource? Icon { get; set; }

Property Value

ImageSource:

The default value is null.

IsActive

Indicates whether the window is currently active.

public bool IsActive { get; protected set; }

Property Value

bool

IsCloseButtonVisible

Indicates whether the window's close button is visible.

public bool IsCloseButtonVisible { get; }

Property Value

bool:

The default value is true.

IsMaximizeButtonVisible

Indicates whether the window's maximize button is visible.

public bool IsMaximizeButtonVisible { get; }

Property Value

bool:

The default value is false.

IsMaximizedFrameVisible

Indicates whether the frame should be displayed when the window is maximized.

public bool IsMaximizedFrameVisible { get; set; }

Property Value

bool:

The default value is true.

IsMinimizeButtonVisible

Indicates whether the window's minimize button is visible.

public bool IsMinimizeButtonVisible { get; }

Property Value

bool:

The default value is false.

IsMoving

Indicates whether the window is currently being dragged to move its location.

public bool IsMoving { get; }

Property Value

bool

IsReadOnly

Indicates whether the window's contents are read-only.

public bool IsReadOnly { get; set; }

Property Value

bool:

The default value is false.

IsResizing

Indicates whether the window is currently being dragged to alter its size.

public bool IsResizing { get; }

Property Value

bool

IsRestoreButtonVisible

Indicates whether the window's restore button is visible.

public bool IsRestoreButtonVisible { get; }

Property Value

bool:

The default value is false.

Left

The position of the window's left edge, in relation to its container.

public double Left { get; set; }

Property Value

double

MaximizeCommand

The ICommand that can maximize the window.

public ICommand MaximizeCommand { get; }

Property Value

ICommand

MinimizeCommand

The ICommand that can minimize the window.

public ICommand MinimizeCommand { get; }

Property Value

ICommand

ReadOnlyContextContentTemplate

The DataTemplate to assign to ContextContentTemplate when the IsReadOnly property is true.

public DataTemplate ReadOnlyContextContentTemplate { get; set; }

Property Value

DataTemplate

ResizeMode

A ResizeMode indicating how the window is able to resize.

public ResizeMode ResizeMode { get; set; }

Property Value

ResizeMode:

The default value is CanResize.

RestoreCommand

The ICommand that can restore the window.

public ICommand RestoreCommand { get; }

Property Value

ICommand

RestoredBounds

The bounds of this window when in a 'restored' state.

public Rect RestoredBounds { get; set; }

Property Value

Rect

ShadowElevation

The shadow elevation.

public int ShadowElevation { get; set; }

Property Value

int:

The default value is 8.

Remarks

Set this property to 0 to disable the shadow.

Title

The title of the window.

[Localizability(LocalizationCategory.Title)]
public string? Title { get; set; }

Property Value

string

TitleBarFontWeight

The FontWeight of the title.

public FontWeight TitleBarFontWeight { get; set; }

Property Value

FontWeight:

The default value is Normal.

Top

The position of the window's top edge, in relation to its container.

public double Top { get; set; }

Property Value

double

WindowState

A WindowState indicating the current state of the window.

public WindowState WindowState { get; set; }

Property Value

WindowState:

The default value is Normal.

Methods

Activate()

Attempts to bring the window to the foreground and activates it.

public virtual bool Activate()

Returns

bool:

true if the window was successfully activated; otherwise, false.

Close()

Closes the window.

public void Close()

DragMove(InputPointerButtonEventArgs)

Allows a window to be dragged by a pointer with its primary button down over an exposed area of the window's client rectangle.

public void DragMove(InputPointerButtonEventArgs e)
Parameter Type Description
e InputPointerButtonEventArgs

The InputPointerButtonEventArgs that can wrap a platform-specific input event args instance.

Remarks

The primary button of the pointer in the event arguments must be pressed down when calling this method. The pointer will be captured.

GetAdjustedBounds(Rect, ResizeOperation)

Returns an adjusted version of the specified bounds, which can provide support for features such as magnetism.

public virtual Rect GetAdjustedBounds(Rect bounds, ResizeOperation resizeOperation)
Parameter Type Description
bounds Rect

The bounds to examine.

resizeOperation ResizeOperation

The active resize operation.

Returns

Rect:

The adjusted bounds.

HitTestResizeOperation(Point)

Hit tests the specified System.Windows.Point to see if it might cause a resize operation.

public ResizeOperation HitTestResizeOperation(Point point)
Parameter Type Description
point Point

The System.Windows.Point, in control coordinates, to hit test.

Returns

ResizeOperation:

The resize operation that can be caused by dragging at the System.Windows.Point.

OnActivated(RoutedEventArgs)

Called when the window gains focus within.

protected virtual void OnActivated(RoutedEventArgs e)
Parameter Type Description
e RoutedEventArgs

The event data.

Remarks

This method has no default implementation. Because an intermediate class in the inheritance might implement this method, we recommend that you call the base implementation in your implementation.

OnApplyTemplate()

When overridden in a derived class, is invoked whenever application code or internal processes call ApplyTemplate().

public override void OnApplyTemplate()

OnClosed(RoutedEventArgs)

Called when the window is about to close.

protected virtual void OnClosed(RoutedEventArgs e)
Parameter Type Description
e RoutedEventArgs

The event data.

Remarks

This method has no default implementation. Because an intermediate class in the inheritance might implement this method, we recommend that you call the base implementation in your implementation.

OnClosing(CancelRoutedEventArgs)

Called directly after Close() is called, and can be handled to cancel window closure.

protected virtual void OnClosing(CancelRoutedEventArgs e)
Parameter Type Description
e CancelRoutedEventArgs

The event data.

Remarks

This method has no default implementation. Because an intermediate class in the inheritance might implement this method, we recommend that you call the base implementation in your implementation.

OnCreateAutomationPeer()

Returns class-specific AutomationPeer implementations for the Windows Presentation Foundation (WPF) infrastructure.

protected override AutomationPeer OnCreateAutomationPeer()

Returns

AutomationPeer:

The type-specific AutomationPeer implementation.

OnDeactivated(RoutedEventArgs)

Called when the window loses focus within.

protected virtual void OnDeactivated(RoutedEventArgs e)
Parameter Type Description
e RoutedEventArgs

The event data.

Remarks

This method has no default implementation. Because an intermediate class in the inheritance might implement this method, we recommend that you call the base implementation in your implementation.

OnDragMoved(RoutedEventArgs)

Called after the window has been moved from a drag.

protected virtual void OnDragMoved(RoutedEventArgs e)
Parameter Type Description
e RoutedEventArgs

The event data.

Remarks

This method has no default implementation. Because an intermediate class in the inheritance might implement this method, we recommend that you call the base implementation in your implementation.

OnDragMoving(CancelRoutedEventArgs)

Called before the window is moved from a drag, and can be handled to cancel the drag.

protected virtual void OnDragMoving(CancelRoutedEventArgs e)
Parameter Type Description
e CancelRoutedEventArgs

The event data.

Remarks

This method has no default implementation. Because an intermediate class in the inheritance might implement this method, we recommend that you call the base implementation in your implementation.

OnDragResized(RoutedEventArgs)

Called after the window has been resized from a drag.

protected virtual void OnDragResized(RoutedEventArgs e)
Parameter Type Description
e RoutedEventArgs

The event data.

Remarks

This method has no default implementation. Because an intermediate class in the inheritance might implement this method, we recommend that you call the base implementation in your implementation.

OnDragResizing(CancelRoutedEventArgs)

Called before the window is resized from a drag, and can be handled to cancel the resize.

protected virtual void OnDragResizing(CancelRoutedEventArgs e)
Parameter Type Description
e CancelRoutedEventArgs

The event data.

Remarks

This method has no default implementation. Because an intermediate class in the inheritance might implement this method, we recommend that you call the base implementation in your implementation.

OnGotKeyboardFocus(KeyboardFocusChangedEventArgs)

Invoked when an unhandled System.Windows.Input.Keyboard.GotKeyboardFocus attached event reaches an element in its route that is derived from this class. Implement this method to add class handling for this event.

protected override void OnGotKeyboardFocus(KeyboardFocusChangedEventArgs e)
Parameter Type Description
e KeyboardFocusChangedEventArgs

The KeyboardFocusChangedEventArgs that contains the event data.

OnLocationChanged(RoutedEventArgs)

Called when the window is moved.

protected virtual void OnLocationChanged(RoutedEventArgs e)
Parameter Type Description
e RoutedEventArgs

The event data.

Remarks

This method has no default implementation. Because an intermediate class in the inheritance might implement this method, we recommend that you call the base implementation in your implementation.

OnLostKeyboardFocus(KeyboardFocusChangedEventArgs)

Invoked when an unhandled System.Windows.Input.Keyboard.LostKeyboardFocus attached event reaches an element in its route that is derived from this class. Implement this method to add class handling for this event.

protected override void OnLostKeyboardFocus(KeyboardFocusChangedEventArgs e)
Parameter Type Description
e KeyboardFocusChangedEventArgs

The KeyboardFocusChangedEventArgs that contains event data.

OnOpened(RoutedEventArgs)

Called when the window is opened.

protected virtual void OnOpened(RoutedEventArgs e)
Parameter Type Description
e RoutedEventArgs

The event data.

Remarks

This method has no default implementation. Because an intermediate class in the inheritance might implement this method, we recommend that you call the base implementation in your implementation.

OnStateChanged(RoutedEventArgs)

Called after the window's WindowState property has changed.

protected virtual void OnStateChanged(RoutedEventArgs e)
Parameter Type Description
e RoutedEventArgs

The event data.

Remarks

This method has no default implementation. Because an intermediate class in the inheritance might implement this method, we recommend that you call the base implementation in your implementation.

OnTitleBarDoubleTapped(CancelRoutedEventArgs)

Called when the title-bar is double-tapped, and can be handled to cancel the default action.

protected virtual void OnTitleBarDoubleTapped(CancelRoutedEventArgs e)
Parameter Type Description
e CancelRoutedEventArgs

The event data.

Remarks

This method has no default implementation. Because an intermediate class in the inheritance might implement this method, we recommend that you call the base implementation in your implementation.

OnTitleBarMenuOpening(DockingMenuEventArgs)

Called when the title-bar should display a context menu.

protected virtual void OnTitleBarMenuOpening(DockingMenuEventArgs e)
Parameter Type Description
e DockingMenuEventArgs

The event data.

Remarks

This method has no default implementation. Because an intermediate class in the inheritance might implement this method, we recommend that you call the base implementation in your implementation.

Show()

Shows the window.

public void Show()

ToggleWindowState()

Toggles the window's state based on its current state.

public void ToggleWindowState()

Remarks

This method is generally called when the title bar is double-tapped.

Events

Activated

Occurs when the window gains focus within.

public event RoutedEventHandler Activated

Event Type

RoutedEventHandler

Closed

Occurs when the window is about to close.

public event RoutedEventHandler Closed

Event Type

RoutedEventHandler

Closing

Occurs directly after Close() is called, and can be handled to cancel window closure.

public event EventHandler<CancelRoutedEventArgs> Closing

Event Type

EventHandler<CancelRoutedEventArgs>

Deactivated

Occurs when the window loses focus within.

public event RoutedEventHandler Deactivated

Event Type

RoutedEventHandler

DragMoved

Occurs after the window has been moved from a drag.

public event RoutedEventHandler DragMoved

Event Type

RoutedEventHandler

DragMoving

Occurs before the window is moved from a drag, and can be handled to cancel the drag.

public event EventHandler<CancelRoutedEventArgs> DragMoving

Event Type

EventHandler<CancelRoutedEventArgs>

DragResized

Occurs after the window has been resized from a drag.

public event RoutedEventHandler DragResized

Event Type

RoutedEventHandler

DragResizing

Occurs before the window is resized from a drag, and can be handled to cancel the resize.

public event EventHandler<CancelRoutedEventArgs> DragResizing

Event Type

EventHandler<CancelRoutedEventArgs>

LocationChanged

Occurs when the window is moved.

public event RoutedEventHandler LocationChanged

Event Type

RoutedEventHandler

Opened

Occurs when the window is opened.

public event RoutedEventHandler Opened

Event Type

RoutedEventHandler

StateChanged

Occurs after the window's WindowState property has changed.

public event RoutedEventHandler StateChanged

Event Type

RoutedEventHandler

TitleBarDoubleTapped

Occurs when the title-bar is double-tapped, and can be handled to cancel the default action.

public event EventHandler<CancelRoutedEventArgs> TitleBarDoubleTapped

Event Type

EventHandler<CancelRoutedEventArgs>

TitleBarMenuOpening

Occurs when the title-bar should display a context menu.

public event EventHandler<DockingMenuEventArgs> TitleBarMenuOpening

Event Type

EventHandler<DockingMenuEventArgs>

Fields

ActivatedEvent

Defines the Activated event.

public static readonly RoutedEvent ActivatedEvent

CanCloseProperty

Defines the CanClose property.

public static readonly DependencyProperty CanCloseProperty

ClosedEvent

Defines the Closed event.

public static readonly RoutedEvent ClosedEvent

ClosingEvent

Defines the Closing event.

public static readonly RoutedEvent ClosingEvent

ContextContentProperty

Defines the ContextContent property.

public static readonly DependencyProperty ContextContentProperty

ContextContentTemplateProperty

Defines the ContextContentTemplate property.

public static readonly DependencyProperty ContextContentTemplateProperty

ContextContentTemplateSelectorProperty

Defines the ContextContentTemplateSelector property.

public static readonly DependencyProperty ContextContentTemplateSelectorProperty

CornerRadiusProperty

Defines the CornerRadius property.

public static readonly DependencyProperty CornerRadiusProperty

DeactivatedEvent

Defines the Deactivated event.

public static readonly RoutedEvent DeactivatedEvent

DragMovedEvent

Defines the DragMoved event.

public static readonly RoutedEvent DragMovedEvent

DragMovingEvent

Defines the DragMoving event.

public static readonly RoutedEvent DragMovingEvent

DragResizedEvent

Defines the DragResized event.

public static readonly RoutedEvent DragResizedEvent

DragResizingEvent

Defines the DragResizing event.

public static readonly RoutedEvent DragResizingEvent

HasCloseButtonProperty

Defines the HasCloseButton property.

public static readonly DependencyProperty HasCloseButtonProperty

HasDropShadowProperty

Defines the HasDropShadow property.

public static readonly DependencyProperty HasDropShadowProperty

HasIconProperty

Defines the HasIcon property.

public static readonly DependencyProperty HasIconProperty

HasMaximizeButtonProperty

Defines the HasMaximizeButton property.

public static readonly DependencyProperty HasMaximizeButtonProperty

HasMinimizeButtonProperty

Defines the HasMinimizeButton property.

public static readonly DependencyProperty HasMinimizeButtonProperty

HasRestoreButtonProperty

Defines the HasRestoreButton property.

public static readonly DependencyProperty HasRestoreButtonProperty

HasTitleBarProperty

Defines the HasTitleBar property.

public static readonly DependencyProperty HasTitleBarProperty

IconProperty

Defines the Icon property.

public static readonly DependencyProperty IconProperty

IsActiveProperty

Defines the IsActive property.

public static readonly DependencyProperty IsActiveProperty

IsCloseButtonVisibleProperty

Defines the IsCloseButtonVisible property.

public static readonly DependencyProperty IsCloseButtonVisibleProperty

IsMaximizeButtonVisibleProperty

Defines the IsMaximizeButtonVisible property.

public static readonly DependencyProperty IsMaximizeButtonVisibleProperty

IsMaximizedFrameVisibleProperty

Defines the IsMaximizedFrameVisible property.

public static readonly DependencyProperty IsMaximizedFrameVisibleProperty

IsMinimizeButtonVisibleProperty

Defines the IsMinimizeButtonVisible property.

public static readonly DependencyProperty IsMinimizeButtonVisibleProperty

IsMovingProperty

Defines the IsMoving property.

public static readonly DependencyProperty IsMovingProperty

IsReadOnlyProperty

Defines the IsReadOnly property.

public static readonly DependencyProperty IsReadOnlyProperty

IsResizingProperty

Defines the IsResizing property.

public static readonly DependencyProperty IsResizingProperty

IsRestoreButtonVisibleProperty

Defines the IsRestoreButtonVisible property.

public static readonly DependencyProperty IsRestoreButtonVisibleProperty

LeftProperty

Defines the Left property.

public static readonly DependencyProperty LeftProperty

LocationChangedEvent

Defines the LocationChanged event.

public static readonly RoutedEvent LocationChangedEvent

OpenedEvent

Defines the Opened event.

public static readonly RoutedEvent OpenedEvent

ReadOnlyContextContentTemplateProperty

Defines the ReadOnlyContextContentTemplate property.

public static readonly DependencyProperty ReadOnlyContextContentTemplateProperty

ResizeModeProperty

Defines the ResizeMode property.

public static readonly DependencyProperty ResizeModeProperty

RestoredBoundsProperty

Defines the RestoredBounds property.

public static readonly DependencyProperty RestoredBoundsProperty

ShadowElevationProperty

Defines the ShadowElevation property.

public static readonly DependencyProperty ShadowElevationProperty

StateChangedEvent

Defines the StateChanged event.

public static readonly RoutedEvent StateChangedEvent

TitleBarDoubleTappedEvent

Defines the TitleBarDoubleTapped event.

public static readonly RoutedEvent TitleBarDoubleTappedEvent

TitleBarFontWeightProperty

Defines the TitleBarFontWeight property.

public static readonly DependencyProperty TitleBarFontWeightProperty

TitleBarMenuOpeningEvent

Defines the TitleBarMenuOpening event.

public static readonly RoutedEvent TitleBarMenuOpeningEvent

TitleProperty

Defines the Title property.

public static readonly DependencyProperty TitleProperty

TopProperty

Defines the Top property.

public static readonly DependencyProperty TopProperty

WindowStateProperty

Defines the WindowState property.

public static readonly DependencyProperty WindowStateProperty

Extension Methods