Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I tried every possible angle for a solution to making a custom brush, from using MarkupExtensions to messing around with TypeConverter attributes, then it dawwned on me: you simply create a wrapper class based on DependencyObject, create a DependencyProperty of type Brush, implement your customization then Bind to the Brush DependencyProperty. </p> <p>Afterwards, put the custom brush in Resources</p> <pre><code> &lt;l:CustomBrush x:Key="CustomBrush" Brush="Magenta" /&gt; </code></pre> <p>Then Bind to it:</p> <pre><code> &lt;Line X1="0" Y1="-5" X2="200" Y2="-5" Stroke="{Binding Source={StaticResource CustomBrush}, Path=Brush}" StrokeThickness="12"/&gt; </code></pre> <p>I think this is better than the MarkupExtension solution because you can't put one in Resources and therefore you have to redefine the custom settings every time you use it.</p> <p>In any case, this would seem to be a simple solution to inheriting from any Wpf object, and with the flexibility of Bindings you can also bind to members of the wrapped Wpf object itself.</p> <p>Here's the simple class:</p> <pre><code>public class CustomBrush : DependencyObject { public CustomBrush() { this.Brush = Brushes.Azure; } #region Brush DependencyProperty [BindableAttribute(true)] public Brush Brush { get { return (Brush)GetValue(BrushProperty); } set { SetValue(BrushProperty, value); } } public static readonly DependencyProperty BrushProperty = DependencyProperty.Register( "Brush", typeof(Brush), typeof(CustomBrush), new UIPropertyMetadata(null)); #endregion } </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