139 lines
4.0 KiB
C#
139 lines
4.0 KiB
C#
|
|
|
|||
|
|
|
|||
|
|
|
|||
|
|
using System.Windows.Controls.Primitives;
|
|||
|
|
|
|||
|
|
|
|||
|
|
// ReSharper disable once CheckNamespace
|
|||
|
|
namespace WPFluent.Controls;
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// Represents a control that creates a pop-up window that displays information for an element in the interface.
|
|||
|
|
/// </summary>
|
|||
|
|
[TemplatePart(Name = "PART_Popup", Type = typeof(Popup))]
|
|||
|
|
public class Flyout : System.Windows.Controls.ContentControl
|
|||
|
|
{
|
|||
|
|
private const string ElementPopup = "PART_Popup";
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// Identifies the <see cref="Closed"/> routed event.
|
|||
|
|
/// </summary>
|
|||
|
|
public static readonly RoutedEvent ClosedEvent = EventManager.RegisterRoutedEvent(
|
|||
|
|
nameof(Closed),
|
|||
|
|
RoutingStrategy.Bubble,
|
|||
|
|
typeof(TypedEventHandler<Flyout, RoutedEventArgs>),
|
|||
|
|
typeof(Flyout));
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// Identifies the <see cref="IsOpen"/> dependency property.
|
|||
|
|
/// </summary>
|
|||
|
|
public static readonly DependencyProperty IsOpenProperty = DependencyProperty.Register(
|
|||
|
|
nameof(IsOpen),
|
|||
|
|
typeof(bool),
|
|||
|
|
typeof(Flyout),
|
|||
|
|
new PropertyMetadata(false));
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// Identifies the <see cref="Opened"/> routed event.
|
|||
|
|
/// </summary>
|
|||
|
|
public static readonly RoutedEvent OpenedEvent = EventManager.RegisterRoutedEvent(
|
|||
|
|
nameof(Opened),
|
|||
|
|
RoutingStrategy.Bubble,
|
|||
|
|
typeof(TypedEventHandler<Flyout, RoutedEventArgs>),
|
|||
|
|
typeof(Flyout));
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// Identifies the <see cref="Placement"/> dependency property.
|
|||
|
|
/// </summary>
|
|||
|
|
public static readonly DependencyProperty PlacementProperty = DependencyProperty.Register(
|
|||
|
|
nameof(Placement),
|
|||
|
|
typeof(PlacementMode),
|
|||
|
|
typeof(Flyout),
|
|||
|
|
new PropertyMetadata(PlacementMode.Top));
|
|||
|
|
|
|||
|
|
private Popup _popup = default;
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// Event triggered when <see cref="Flyout"/> is opened.
|
|||
|
|
/// </summary>
|
|||
|
|
public event TypedEventHandler<Flyout, RoutedEventArgs> Closed
|
|||
|
|
{
|
|||
|
|
add => AddHandler(ClosedEvent, value);
|
|||
|
|
remove => RemoveHandler(ClosedEvent, value);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// Event triggered when <see cref="Flyout"/> is opened.
|
|||
|
|
/// </summary>
|
|||
|
|
public event TypedEventHandler<Flyout, RoutedEventArgs> Opened
|
|||
|
|
{
|
|||
|
|
add => AddHandler(OpenedEvent, value);
|
|||
|
|
remove => RemoveHandler(OpenedEvent, value);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
protected virtual void OnPopupClosed(object sender, EventArgs e)
|
|||
|
|
{
|
|||
|
|
Hide();
|
|||
|
|
RaiseEvent(new RoutedEventArgs(ClosedEvent, this));
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
protected virtual void OnPopupOpened(object sender, EventArgs e)
|
|||
|
|
{ RaiseEvent(new RoutedEventArgs(OpenedEvent, this)); }
|
|||
|
|
|
|||
|
|
public void Hide()
|
|||
|
|
{
|
|||
|
|
if(IsOpen)
|
|||
|
|
{
|
|||
|
|
SetCurrentValue(IsOpenProperty, false);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// Invoked whenever application code or an internal process, such as a rebuilding layout pass, calls the
|
|||
|
|
/// ApplyTemplate method.
|
|||
|
|
/// </summary>
|
|||
|
|
public override void OnApplyTemplate()
|
|||
|
|
{
|
|||
|
|
base.OnApplyTemplate();
|
|||
|
|
|
|||
|
|
_popup = GetTemplateChild(ElementPopup) as Popup;
|
|||
|
|
|
|||
|
|
if(_popup is null)
|
|||
|
|
{
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
_popup.Opened -= OnPopupOpened;
|
|||
|
|
_popup.Opened += OnPopupOpened;
|
|||
|
|
|
|||
|
|
_popup.Closed -= OnPopupClosed;
|
|||
|
|
_popup.Closed += OnPopupClosed;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public void Show()
|
|||
|
|
{
|
|||
|
|
if(!IsOpen)
|
|||
|
|
{
|
|||
|
|
SetCurrentValue(IsOpenProperty, true);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// Gets or sets a value indicating whether a <see cref="Flyout"/> is visible.
|
|||
|
|
/// </summary>
|
|||
|
|
public bool IsOpen { get => (bool)GetValue(IsOpenProperty); set => SetValue(IsOpenProperty, value); }
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// Gets or sets the orientation of the <see cref="Flyout"/> control when the control opens, and specifies the
|
|||
|
|
/// behavior of the <see cref="T:System.Windows.Controls.Primitives.Popup"/> control when it overlaps screen
|
|||
|
|
/// boundaries.
|
|||
|
|
/// </summary>
|
|||
|
|
[Bindable(true)]
|
|||
|
|
[Category("Layout")]
|
|||
|
|
public PlacementMode Placement
|
|||
|
|
{
|
|||
|
|
get => (PlacementMode)GetValue(PlacementProperty);
|
|||
|
|
set => SetValue(PlacementProperty, value);
|
|||
|
|
}
|
|||
|
|
}
|