How to get the default template of a control programmatically ?

To get the Style for any given WPF control, get the control instance and use the ‘XAMLWriter’ class for writing the XAML related information. [C#] System.Windows.Controls.TextBox t = new System.Windows.Controls.TextBox(); StringBuilder sb = new StringBuilder(); using (TextWriter writer = new StringWriter(sb)) { System.Windows.Markup.XamlWriter.Save(t.Template, writer); }

How do I remove the Adorner from an element ?

1. Call the static method ‘GetAdornerLayer()’, to get the AdornerLayer associated with the UIElement. 2. Call the ‘Add()’ method to bind the Adorner to the target UIElement. [C#] AdornerLayer adornerLayer = AdornerLayer.GetAdornerLayer(myTextBox); adornerLayer.Remove(new ControlAdorner(myTextBox));

How can I map CLR Namespaces to XML Namespaces in an Assembly ?

WPF defines a CLR attribute that is consumed by XAML processors in order to map multiple CLR namespaces to a single XML namespace. The ‘xlmns’ Definition Attribute is placed at the assembly level in the source code that produces the assembly. The WPF assembly source code uses this attribute to map the various common namespaces such as System.Windows and System.Windows.Controls to the http://schemas.microsoft.com/winfx/2006/xaml/presentation namespace. The ’xmlns’ Definition Attribute takes two parameters: the XML namespace name and the CLR namespace name. More than one ’xmlns’ Definition Attribute can exist to map multiple CLR namespaces to the same XML namespace. Once mapped, members of those namespaces can also be referenced without full qualification if desired, by providing the appropriate using statement in the partial-class code-behind page. For more details, see ’xmlns Definition Attribute’.

How do I put controls such as a ProgressBar into a StatusBar ?

You cannot place controls into a StatusBar control in the Designer. However, you can add any number of controls to the StatusBar programmatically through it’s ‘Controls’ property. After adding the controls, set their Visible, Location, Bounds and other properties. Here’s a sample method that could be called in a form’s constructor after the ’InitializeComponent’. [C#] private void AddStatusBarControls(StatusBar sb) { ProgressBar pb = new ProgressBar(); sb.Controls.Add(pb); pb.Visible = true; pb.Bounds = new Rectangle(sb.Width / 4, 4, sb.Width / 2, sb.Height – 8); pb.Anchor = AnchorStyles.Left | AnchorStyles.Right; }

How do I add custom drawing to a Button?

‘Subclass Button’ and adding a custom ‘Paint’ event handler lets you do this. [C#] public class CustomButton : Button { public CustomButton() { Paint += new PaintEventHandler( ButtonPaint ); } private void ButtonPaint( object sender, PaintEventArgs e ) { Pen pen = new Pen( Color.Red ); pen.Width = 8; e.Graphics.DrawLine( pen, 7, 4, 7, Height – 4 ); pen.Width = 1; e.Graphics.DrawEllipse( pen, Width – 16 , 6, 8, 8 ); } }