Reputation: 1800
For example:
public class DesignerPatternBrush : Brush
{
public string Name { get; set; }
}
I would like to define my own Brush
class by adding a new property called Name
, but there is a compiler error:
error CS0534: 'Brushes.DesignerPatternBrush' does not implement inherited abstract member 'System.Windows.Freezable.CreateInstanceCore()'
How can I add a Name
property to a Brush
type?
Note this related question: How do I implement a custom Brush in WPF? It answers the question of why it is not possible to literally inherit Brush
. But is there some other way (e.g. using an attached property) to achieve the same effect I want?
Upvotes: 7
Views: 925
Reputation: 1
I have a simple solution: Instead of inheritance, you can create a class that alters a Brush properties throw attached properties. For example: I created a class named "HatchBrushes" that can create 55 DrawingBrushes with deferent hatch styles (similar to WinForms HatchBrush.. In fact this part of code belongs to another programer) The HatchBrushes class defines 4 attached properties that controls the hatch brush appearance: HatchStyle, Background, Foreground, and PenThickness. All these properties register a PropertyChangedCallBck sub named "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 wont write it here because it is too long.
Now, I can color the background of the window with a Horizontal-lines Hatch with simole xaml code like this:
<DrawingBrush c:HatchBrushes.HatchStyle="Horizontal"
c:HatchBrushes.Background="Red"
c:HatchBrushes.Foreground="Yellow"
c:HatchBrushes.PenThickness="2"/>
Upvotes: 0
Reputation: 5566
As the compile error message says is Brush an abstract class --> you have to implement its abstract members
to let Visual Studio do all the work for you there exist a shortcut.
Select the Brush class and press Alt + Shift + F10 --> Enter the abstract class gets 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 auto-implements all methods which are visible, but the Brush class defines some methods as internal abstract
i.e:
internal abstract int GetChannelCountCore();
As the Brush is defined in PresentationCore we will never be able to override the abstract method outside the assembly... --> impossible to inherit from the class
Upvotes: 2
Reputation: 10957
It means you need to implement all abstract methods that were inherited from Brush and its ancestors. In this case it's CreateInstanceCore()
method of the Freezable
class.
Why do you need a named brush? You can create a brush and store it into a ResourceDictionary (under a given key, which is basically a name) for your view/window/application - maybe you are looking for this solution.
More on that here: http://blogs.msdn.com/b/wpfsldesigner/archive/2010/06/03/creating-and-consuming-resource-dictionaries-in-wpf-and-silverlight.aspx
Upvotes: 3
Reputation: 6444
When you inherit an abstract class you have some members that you need to Override
. You will notice a little dropdown under Brush
. If you press CTRL
+ .
whilst your cursor is on Brush
it will ask you to implement it, I press Enter
afterCTRL
+ Space
. When you implement it you will see this:
protected override Freezable CreateInstanceCore()
{
throw new NotImplementedException();
}
Upvotes: 1