Flyouts

Overview

Flyouts are light dismissable containers that show arbitrary UI content. Flyouts are not controls and can be declared as a resource and shared between multiple elements within your app.

FlyoutBase

FlyoutBase is the base class for all flyout implementations.

Common Properties for all Flyouts

Property
Description
IsOpen
Gets whether the Flyout is currently open.
Placement
Gets or sets where the Flyout opens relative to its target
ShowMode
Gets or sets the desired show mode of the Flyout, which determines if it is a transient (no focus) UI or not
Target
Gets the target the Flyout is currently assigned to

Common Methods for all Flyouts

Property
Description
ShowAt(Control)
Shows the Flyout at the specified target
ShowAt(Control, bool)
Shows the Flyout at the specified target, but places it at the current pointer position
Hide
Hides the Flyout

Reference

​FlyoutBase​

Source code

​FlyoutBase.cs​

Flyout Types

There are two built-in types of Flyouts: Flyout and MenuFlyout. A regular Flyout has no special logic and is just a simple container for any abitrary UI content.
Basic Flyout
MenuFlyout, as the name implies, creates a Menu.
Basic MenuFlyout

Reference

​Flyout​
​MenuFlyout​

Source code

​Flyout.cs​
​MenuFlyout.cs​

Creating Flyouts

In order to be shown Flyouts have to be attached to a specific control, though this is not a static assignment and can be changed at runtime. Button has a Flyout property that can be used open a Flyout upon click.
1
<Button Content="Click me">
2
<Button.Flyout>
3
<Flyout>
4
<TextBlock Text="Flyout Content!" />
5
</Flyout>
6
</Button.Flyout>
7
</Button>
Copied!

Attached Flyouts

For other controls that don't have built-in support for flyouts, one can be assigned using attached flyouts
1
<Border Background="Red" PointerPressed="Border_PointerPressed">
2
<FlyoutBase.AttachedFlyout>
3
<Flyout>
4
<TextBlock Text="AttachedFlyout Content!" />
5
</Flyout>
6
</FlyoutBase.AttachedFlyout>
7
</Border>
Copied!
Attached Flyouts can be shown by calling the ShowAttachedFlyout method
1
public void Border_PointerPressed(object sender, RoutedEventArgs args)
2
{
3
FlyoutBase.ShowAttachedFlyout(sender as Control);
4
}
Copied!

Context Flyouts

Controls can also utilize Context Flyouts, which is an alternative/replacement to ContextMenus that provides a sharable, richer UI experience than simple context menus. NOTE: A control cannot have both a ContextFlyout and ContextMenu at the same time.
ContextFlyouts are invoked automatically like normal ContextMenus. Although custom behaviors and logic an be implemented by invoking it manually (like any other flyout: ContextFlyout.ShowAt(Control)) or responding to the ContextRequested event

Sharing Flyouts

As previously mentioned, Flyouts can be shared between various elements within your app.
1
<Window.Resources>
2
<Flyout x:Key="MySharedFlyout">
3
<!-- Flyout content here -->
4
</Flyout>
5
</Window.Resources>
6
​
7
<Button Content="Click me!" Flyout="{StaticResource MySharedFlyout}" />
8
​
9
<Button Content="Now click me!" Flyout="{StaticResource MySharedFlyout}" />
Copied!

Styling Flyouts

Although Flyouts are not controls themselves, their general appearance can still be customized by targeting the presenter the Flyout uses to display its content. For a normal Flyout this is FlyoutPresenter and for MenuFlyout this is MenuFlyoutPresenter. Because flyout presenters are not exposed, special style classes that should pertain to specific flyouts can be passed using the FlyoutPresenterClasses property on FlyoutBase
1
<Style Selector="FlyoutPresenter.mySpecialClass">
2
<Setter Property="Background" Value="Red" />
3
</Style>
4
​
5
<Flyout FlyoutPresenterClasses="mySpecialClass">
6
<!-- Flyout content here -->
7
</Flyout>
Copied!

Creating Custom Flyouts

To create a custom flyout type, derive from FlyoutBase. You'll have to override the abstract method CreatePresenter() to specify the presenter the Flyout should use to display its content. This can be any type of control, but note that this is the root content for the inner popup and should be styled with background, border, corner radius, etc. to match other popups. You can still use a normal FlyoutPresenter if you wish
The following examle creates a simple Flyout that hosts an image
1
public class MyImageFlyout : FlyoutBase
2
{
3
public static readonly StyledProperty<IImage> ImageProperty = AvaloniaProperty.Register<MyImageFlyout, IImage>(nameof(Image));
4
​
5
[Content]
6
public IImage Image { get; set; }
7
​
8
protected override Control CreatePresenter()
9
{
10
// In this example, we'll use the default FlyoutPresenter as the root content, and add an Image control to show our content
11
return new FlyoutPresenter
12
{
13
Content = new Image
14
{
15
// Use binding here so the image automatically updates when the property updates
16
[!Image.SourceProperty] = this[!ImageProperty]
17
}
18
};
19
}
20
}
Copied!
Last modified 3mo ago