Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here's a sample (type something into the textbox and watch the adorner):</p> <p>Code:</p> <pre><code>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.Navigation; using System.Windows.Shapes; using System.Globalization; namespace Adorners { /// &lt;summary&gt; /// Interaction logic for MainWindow.xaml /// &lt;/summary&gt; public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); this.Loaded += (o, e) =&gt; { AdornerLayer layer = AdornerLayer.GetAdornerLayer(this.t); MyAdorner myAdorner = new MyAdorner(this.t); layer.Add(myAdorner); this.t.Text = "Modified Value"; }; } } public class MyAdorner : Adorner { public static DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(MyAdorner), new PropertyMetadata("Default Text", (o, e) =&gt; { ((MyAdorner)o).InvalidateVisual(); })); // Be sure to call the base class constructor. public MyAdorner(UIElement adornedElement) : base(adornedElement) { this.DataContext = this.AdornedElement; this.SetUpBindings(); } private void SetUpBindings() { BindingOperations.SetBinding(this, MyAdorner.TextProperty, new Binding() { Path = new PropertyPath("Text"), UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged }); } public string Text { get { return (string)this.GetValue(MyAdorner.TextProperty); } set { this.SetValue(MyAdorner.TextProperty, value); } } protected override void OnRender(DrawingContext drawingContext) { Rect adornedElementRect = new Rect(this.AdornedElement.DesiredSize); drawingContext.DrawText(new FormattedText(this.Text, CultureInfo.CurrentCulture, FlowDirection.LeftToRight, new Typeface("Arial"), 20, Brushes.Black), new Point(0, 150)); } } } </code></pre> <p>Markup:</p> <pre><code>&lt;Window x:Class="Adorners.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"&gt; &lt;Grid x:Name="AdornedGrid"&gt; &lt;TextBox x:Name="t" Width="200" Height="100" Background="Green"&gt;&lt;/TextBox&gt; &lt;/Grid&gt; &lt;/Window&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload