Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
swdev:dotnet:adorner_layer [2011/01/10 12:32] smayr [References] |
swdev:dotnet:adorner_layer [2011/01/10 12:47] (current) smayr [Loading Adorner] |
||
---|---|---|---|
Line 1: | Line 1: | ||
== Adorner Layer == | == Adorner Layer == | ||
+ | |||
+ | === Loading Adorner === | ||
+ | |||
+ | Create an adorner. In C# (LoadingAdorner.cs): | ||
+ | <code csharp> | ||
+ | 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, | ||
+ | } | ||
+ | |||
+ | /// < | ||
+ | /// Gets or sets the color to paint | ||
+ | /// </ | ||
+ | public static readonly DependencyProperty ColorProperty = | ||
+ | DependencyProperty.Register(" | ||
+ | new PropertyMetadata((Brush)new BrushConverter().ConvertFromString("# | ||
+ | |||
+ | |||
+ | /// < | ||
+ | /// Gets or sets the border | ||
+ | /// </ | ||
+ | public Pen Border | ||
+ | { | ||
+ | get { return (Pen)GetValue(BorderProperty); | ||
+ | set { SetValue(BorderProperty, | ||
+ | } | ||
+ | |||
+ | /// < | ||
+ | /// Gets or sets the border | ||
+ | /// </ | ||
+ | public static readonly DependencyProperty BorderProperty = | ||
+ | DependencyProperty.Register(" | ||
+ | new UIPropertyMetadata(new Pen(Brushes.Gray, | ||
+ | |||
+ | //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, | ||
+ | } | ||
+ | |||
+ | /// < | ||
+ | /// Gets or sets the text to display | ||
+ | /// </ | ||
+ | public static readonly DependencyProperty OverlayedTextProperty = | ||
+ | DependencyProperty.Register(" | ||
+ | |||
+ | /// < | ||
+ | /// Gets or sets the foreground to use for the text | ||
+ | /// </ | ||
+ | public Brush ForeGround | ||
+ | { | ||
+ | get { return (Brush)GetValue(ForeGroundProperty); | ||
+ | set { SetValue(ForeGroundProperty, | ||
+ | } | ||
+ | |||
+ | /// < | ||
+ | /// Gets or sets the foreground to use for the text | ||
+ | /// </ | ||
+ | public static readonly DependencyProperty ForeGroundProperty = | ||
+ | DependencyProperty.Register(" | ||
+ | new UIPropertyMetadata(Brushes.Black)); | ||
+ | |||
+ | |||
+ | /// < | ||
+ | /// Gets or sets the font size for the text | ||
+ | /// </ | ||
+ | public double FontSize | ||
+ | { | ||
+ | get { return (double)GetValue(FontSizeProperty); | ||
+ | set { SetValue(FontSizeProperty, | ||
+ | } | ||
+ | |||
+ | /// < | ||
+ | /// Gets or sets the font size for the text | ||
+ | /// </ | ||
+ | public static readonly DependencyProperty FontSizeProperty = | ||
+ | DependencyProperty.Register(" | ||
+ | |||
+ | |||
+ | /// < | ||
+ | /// Gets or sets the Typeface for the text | ||
+ | /// </ | ||
+ | public Typeface Typeface | ||
+ | { | ||
+ | get { return (Typeface)GetValue(TypefaceProperty); | ||
+ | set { SetValue(TypefaceProperty, | ||
+ | } | ||
+ | |||
+ | /// < | ||
+ | /// Gets or sets the Typeface for the text | ||
+ | /// </ | ||
+ | public static readonly DependencyProperty TypefaceProperty = | ||
+ | DependencyProperty.Register(" | ||
+ | new UIPropertyMetadata(new Typeface(" | ||
+ | |||
+ | |||
+ | |||
+ | #endregion | ||
+ | |||
+ | /// < | ||
+ | /// Constructor for the adorner | ||
+ | /// </ | ||
+ | /// <param name=" | ||
+ | public LoadingAdorner(UIElement adornerElement) | ||
+ | : base(adornerElement) | ||
+ | { } | ||
+ | |||
+ | |||
+ | /// < | ||
+ | /// Called to draw on screen | ||
+ | /// </ | ||
+ | /// <param name=" | ||
+ | protected override void OnRender(System.Windows.Media.DrawingContext drawingContext) | ||
+ | { | ||
+ | FormattedText text = new FormattedText(OverlayedText, | ||
+ | FlowDirection.LeftToRight, | ||
+ | drawingContext.DrawText(text, | ||
+ | drawingContext.DrawRectangle(Color, | ||
+ | base.OnRender(drawingContext); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Get Adorner Layer of a control, then add adorner: | ||
+ | <code csharp> | ||
+ | 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 " | ||
+ | LoadingAdorner loading = new LoadingAdorner(mainPane); | ||
+ | loading.FontSize = 15; | ||
+ | loading.OverlayedText = " | ||
+ | loading.Typeface = new Typeface(FontFamily, | ||
+ | FontWeights.Bold, | ||
+ | Binding bind = new Binding(" | ||
+ | bind.Source = controller; | ||
+ | bind.Converter = new VisibilityConverter(); | ||
+ | loading.SetBinding(LoadingAdorner.VisibilityProperty, | ||
+ | | ||
+ | // Get mainPane' | ||
+ | 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 == | == References == | ||
Line 8: | Line 233: | ||
* [[http:// | * [[http:// | ||
* [[http:// | * [[http:// | ||
+ | * NBD-Tech: | ||
+ | * [[http:// | ||
+ | * [[http:// | ||
+ | * [[http:// | ||
+ | * [[http:// |