MarkupExtensions を使用することから TypeConverter 属性をいじることまで、カスタム ブラシを作成するための解決策として考えられるすべての角度を試してみたところ、気が遠くなりました。単に DependencyObject に基づいてラッパー クラスを作成し、Brush タイプの DependencyProperty を作成し、カスタマイズを実装するだけです。次に、Brush DependencyProperty にバインドします。
その後、リソースにカスタム ブラシを配置します。
<l:CustomBrush x:Key="CustomBrush" Brush="Magenta" />
次にそれにバインドします。
<Line X1="0" Y1="-5" X2="200" Y2="-5"
Stroke="{Binding Source={StaticResource CustomBrush}, Path=Brush}"
StrokeThickness="12"/>
これは MarkupExtension ソリューションよりも優れていると思います。これは、リソースに配置できないため、使用するたびにカスタム設定を再定義する必要があるためです。
いずれにせよ、これは任意の Wpf オブジェクトから継承するための簡単な解決策のように思われます。Bindings の柔軟性により、ラップされた Wpf オブジェクト自体のメンバーにバインドすることもできます。
簡単なクラスは次のとおりです。
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
}