== Adorner Layer ==
=== Loading Adorner ===
Create an adorner. In C# (LoadingAdorner.cs):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Documents;
using System.Windows;
using System.Windows.Media;
using System.Threading;
namespace LoadingAdorner
{
///
/// Adorner that disables all controls that fall under it
///
public class LoadingAdorner : Adorner
{
#region Properties
///
/// Gets or sets the color to paint
///
public Brush Color
{
get { return (Brush)GetValue(ColorProperty); }
set { SetValue(ColorProperty, value); }
}
///
/// Gets or sets the color to paint
///
public static readonly DependencyProperty ColorProperty =
DependencyProperty.Register("Color", typeof(Brush), typeof(LoadingAdorner),
new PropertyMetadata((Brush)new BrushConverter().ConvertFromString("#7F4047F7")));
///
/// Gets or sets the border
///
public Pen Border
{
get { return (Pen)GetValue(BorderProperty); }
set { SetValue(BorderProperty, value); }
}
///
/// Gets or sets the border
///
public static readonly DependencyProperty BorderProperty =
DependencyProperty.Register("Border", typeof(Pen), typeof(LoadingAdorner),
new UIPropertyMetadata(new Pen(Brushes.Gray, 1)));
//the start point where to start drawing
private static readonly Point startPoint =
new Point(0, 0);
///
/// Gets or sets the text to display
///
public string OverlayedText
{
get { return (string)GetValue(OverlayedTextProperty); }
set { SetValue(OverlayedTextProperty, value); }
}
///
/// Gets or sets the text to display
///
public static readonly DependencyProperty OverlayedTextProperty =
DependencyProperty.Register("OverlayedText", typeof(string), typeof(LoadingAdorner), new UIPropertyMetadata(""));
///
/// Gets or sets the foreground to use for the text
///
public Brush ForeGround
{
get { return (Brush)GetValue(ForeGroundProperty); }
set { SetValue(ForeGroundProperty, value); }
}
///
/// Gets or sets the foreground to use for the text
///
public static readonly DependencyProperty ForeGroundProperty =
DependencyProperty.Register("ForeGround", typeof(Brush), typeof(LoadingAdorner),
new UIPropertyMetadata(Brushes.Black));
///
/// Gets or sets the font size for the text
///
public double FontSize
{
get { return (double)GetValue(FontSizeProperty); }
set { SetValue(FontSizeProperty, value); }
}
///
/// Gets or sets the font size for the text
///
public static readonly DependencyProperty FontSizeProperty =
DependencyProperty.Register("FontSize", typeof(double), typeof(LoadingAdorner), new UIPropertyMetadata(10.0));
///
/// Gets or sets the Typeface for the text
///
public Typeface Typeface
{
get { return (Typeface)GetValue(TypefaceProperty); }
set { SetValue(TypefaceProperty, value); }
}
///
/// Gets or sets the Typeface for the text
///
public static readonly DependencyProperty TypefaceProperty =
DependencyProperty.Register("Typeface", typeof(Typeface), typeof(LoadingAdorner),
new UIPropertyMetadata(new Typeface("Verdana")));
#endregion
///
/// Constructor for the adorner
///
/// The element to be adorned
public LoadingAdorner(UIElement adornerElement)
: base(adornerElement)
{ }
///
/// Called to draw on screen
///
/// The drawind context in which we can draw
protected override void OnRender(System.Windows.Media.DrawingContext drawingContext)
{
FormattedText text = new FormattedText(OverlayedText, Thread.CurrentThread.CurrentUICulture,
FlowDirection.LeftToRight, Typeface, FontSize, ForeGround);
drawingContext.DrawText(text, startPoint);
drawingContext.DrawRectangle(Color, Border, new Rect(startPoint, DesiredSize));
base.OnRender(drawingContext);
}
}
}
Get Adorner Layer of a control, then add adorner:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace LoadingAdorner
{
///
/// Interaction logic for DemoPage.xaml
///
public partial class DemoPage : Window
{
DemoPageController controller = new DemoPageController();
public DemoPage()
{
DataContext = controller.Data;
InitializeComponent();
AttachLoadingAdorner();
}
private void AttachLoadingAdorner()
{
// Create "Loading" adorner
LoadingAdorner loading = new LoadingAdorner(mainPane);
loading.FontSize = 15;
loading.OverlayedText = "loading...";
loading.Typeface = new Typeface(FontFamily, FontStyles.Italic,
FontWeights.Bold, FontStretch);
Binding bind = new Binding("SearchInProgress");
bind.Source = controller;
bind.Converter = new VisibilityConverter();
loading.SetBinding(LoadingAdorner.VisibilityProperty, bind);
// Get mainPane's AdornerLayer and add new "Loading" adorner
AdornerLayer.GetAdornerLayer(mainPane).Add(loading);
}
}
///
/// Converter that converts a bool to a Visibility status
///
public class VisibilityConverter : IValueConverter
{
#region IValueConverter Members
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return (bool)value ? Visibility.Visible : Visibility.Collapsed;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
#endregion
}
}
== References ==
* [[http://msdn.microsoft.com/en-us/library/ms753340.aspx|MSDN: Adorners How-to Topics]]
* [[http://msdn.microsoft.com/en-us/library/ms743737.aspx|MSDN: Adorners Overview]]
* [[http://www.codeproject.com/KB/WPF/WPF_Loading_Wait_Adorner.aspx|Loading Wait Adorner]]
* [[http://joshsmithonwpf.wordpress.com/2007/08/25/rendering-text-in-the-adorner-layer/|John Smith on WPF: Rendering Text in Adorner Layer]]
* [[http://marlongrech.wordpress.com/2008/02/28/wpf-overlays-or-better-adorner/|WPF Overlays or Better Adorner]]
* [[http://denisvuyka.wordpress.com/2007/10/15/wpf-simple-adorner-usage-with-drag-and-resize-operations/|WPF Simple Adorner Usage (Drag & Resize Operations)]]
* NBD-Tech:
* [[http://www.nbdtech.com/Blog/archive/2010/06/21/wpf-adorners-part-1-ndash-what-are-adorners.aspx|WPF Adorners (Part 1): What are Adorners]]
* [[http://www.nbdtech.com/Blog/archive/2010/06/28/wpf-adorners-part-2-ndash-placing-any-control-on-the.aspx|WPF Adorners (Part 2): Placing any control on adorner layer]]
* [[http://www.nbdtech.com/Blog/archive/2010/07/05/wpf-adorners-part-3-ndash-adorners-and-validation.aspx|WPF Adorners (Part 3): Adorners and validation]]
* [[http://www.nbdtech.com/Blog/archive/2010/07/12/wpf-adorners-part-4-ndash-simple-and-powerful-system-for.aspx|WPF Adorners (Part 4): Simple and Powerful System for Adorners (Bubble Popup)]]