7

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?

Community
  • 1
  • 1
dongx
  • 1,750
  • 4
  • 17
  • 37

4 Answers4

3

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

Honza Brestan
  • 10,637
  • 2
  • 32
  • 43
  • 1
    I tried, but how to retrieve the Name or Key after creating a brush object. Can I do something like the following? Create a brush object: brush = userControl.FindResource("MyBrush1"). To retrieve the name of the brush: brush.Name – dongx Nov 20 '12 at 16:11
2

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

fixagon
  • 5,506
  • 22
  • 26
  • Just doing this won't work, there are a bunch of missing abstract methods in brush that I don't seem to see in the brush declaration. GetChannelCountCore(), GetChannelCore(int), AddrefOnChannelCore... at least didn't work for me anyway – Andy Nov 20 '12 at 16:06
  • 1
    Didn't know you could do `Alt` + `Shift` + `F10` as well as `Ctrl` + `.` - not sure if you did either :P – LukeHennerley Nov 20 '12 at 16:06
  • no i didnt know. sounds strange but i find alt + shift + f10 quite intuitive ;) – fixagon Nov 20 '12 at 16:09
  • @Andy This works for me, by implementing it it only includes one member and then it allows me to build. – LukeHennerley Nov 20 '12 at 16:10
  • @fix_likes_coding How?! It's such a ball ache extending your fingers across the keyboard for three keys :P – LukeHennerley Nov 20 '12 at 16:11
  • Ok, I must have something else going on then. – Andy Nov 20 '12 at 16:11
  • Looks like inherit from Brush is too difficult. @Andy – dongx Nov 20 '12 at 16:14
  • @Andy wait a sec, im just testing something. i think its linked to the different assemblys in the System.Windows Namespace – fixagon Nov 20 '12 at 16:14
  • 1
    seems it's not just the brush that does this Transform and Geometry are the same. – Andy Nov 20 '12 at 16:30
1

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();
  }
LukeHennerley
  • 6,344
  • 1
  • 32
  • 50
0

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"/>
  • _"I wont write it here because it is too long"_ -- that's unfortunate, because that would make the difference between a useful answer and a non-useful one. Note that the OP is actually asking just to add a `Name` property, so you _could_ have demonstrated _that_ instead of introducing the more-complex "hatched brush" scenario. Please consider improving your answer, as frankly it's the only one that's even come close to actually _helping_ the OP accomplish their goal. – Peter Duniho May 27 '15 at 20:57