How to inherit from Brush, so I can add the Name property?

For example:

public class DesignerPatternBrush : Brush { public string Name { get; set; } } 

I would like to define my own Brush class by adding a new Name property, but there is a compiler error:

error CS0534: "Brushes.DesignerPatternBrush" does not implement the inherited abstract element "System.Windows.Freezable.CreateInstanceCore ()"

How to add Name property to Brush type?

Pay attention to this related question: How to implement custom Brush in WPF? He answers the question of why it is impossible to inherit Brush literally. But is there any other way (e.g. using an attached property) to achieve the same effect that I want?

+7
source share
4 answers

As the compilation error message says: Brush abstract class -> you need to implement its abstract elements

for Visual Studio to do all the work for you, there is a shortcut.

Select the Brush class and press Alt + Shift + F10 → Enter; the abstract class is automatically implemented:

 public class T : Brush { protected override Freezable CreateInstanceCore() { throw new NotImplementedException(); } } 

EDIT:

but this will not work with the Brush class. Visual Studio automatically implements all the methods that are visible, but the Brush class defines some methods as internal abstract

i.e:

 internal abstract int GetChannelCountCore(); 

Since Brush is defined in PresentationCore, we can never override the abstract method outside the assembly ... → it is impossible to inherit from the class

+2
source

This means that you need to implement all the abstract methods that were inherited from Brush and his ancestors. In this case, this is the CreateInstanceCore() method of the Freezable class.

Why do you need a named brush? You can create a brush and save it in a ResourceDictionary (under a given key, which is basically a name) for your view / window / application - perhaps you are looking for this solution.

More about this here: http://blogs.msdn.com/b/wpfsldesigner/archive/2010/06/03/creating-and-consuming-resource-dictionaries-in-wpf-and-silverlight.aspx

+3
source

When you inherit an abstract class, you have members that need an Override . You will see a small drop-down menu under Brush . If you press CTRL + . while your cursor is on Brush , it will ask you to implement it, press Enter after CTRL + Space . When you implement it, you will see the following:

  protected override Freezable CreateInstanceCore() { throw new NotImplementedException(); } 
+1
source

I have a simple solution: instead of inheriting, you can create a class that modifies the Brush properties by inserting attached properties. For example: I created a class called "HatchBrushes" that can create 55 DrawingBrushes with delayed hatching styles (similar to WinForms HatchBrush .. Actually this part of the code belongs to another programmer) The HatchBrushes class defines 4 attached properties that control the appearance of the hatch: HatchStyle , Background, Foreground, and PenThickness. All of these properties register a subclass of PropertyChangedCallBck called "OnHatchChanged", where I can change the properties of the DrawingBrush:

 Shared Sub OnHatchChanged(d As DependencyObject, e As DependencyPropertyChangedEventArgs) Dim DBrush = TryCast(d, DrawingBrush) If DBrush Is Nothing Then Return Dim B = GetHatchBrush(GetHatchStyle(DBrush), GetBackground(DBrush), GetForeground(DBrush), GetPenThickness(DBrush)) DBrush.Drawing = B.Drawing.CloneCurrentValue DBrush.Stretch = B.Stretch DBrush.ViewportUnits = B.ViewportUnits DBrush.Viewport = B.Viewport DBrush.TileMode = B.TileMode End Sub 

Note that "GetHatchBrush" is a function that creates a DrawingBrush with the desired HatchStyle. I will not write it here because it is too long.

Now I can color the window background using the Horizontal-Line Hatch with simole xaml code as follows:

 <DrawingBrush c:HatchBrushes.HatchStyle="Horizontal" c:HatchBrushes.Background="Red" c:HatchBrushes.Foreground="Yellow" c:HatchBrushes.PenThickness="2"/> 
0
source

All Articles