6

In WPF I would like to create custom Inline implementation. From documentation of Inline: "An abstract class that provides a base for all inline flow content elements." Classes like Figure, Run or Span inherit from Inline.

My custom class inheriting from Inline would be something like '2 lined Run'. I have special needs for flow of document and this seems to be the only way. However I don't know where to start: Inline does not define any members! It is abstract class so it is meant to be inherited but there is no documentation on how to inherit from it. Not in MSDN and nowhere else where I could find it.

If you can provide some on-line resources (tutorial/blog/article) or code sample how to create subclass of Inline. For example just empty box of some width and height.

If you want to know why I want to create custom Inline element have a look on question Create guitar chords editor in WPF.

Community
  • 1
  • 1
Rasto
  • 17,204
  • 47
  • 154
  • 245

1 Answers1

2

Base classes can be used for the mere purposes of adding type fidelity. For example, code in a FlowDocument processor might just want to do code like:

if(currentElement is Inline)
{
   // Do something
}

Inline doesn't actually have to do anything at all.

As far as subclassing from Inline, I think you might not be able to achieve what you want. My understanding is that the FlowDocument renderer is responsible for looking at the types of the elements and interpreting how they behave from its type and its properties. In other words, it has no knowledge of your custom code. I think the best you could do is to subclass from a useful element and have your subclass mess with property values or anything that is overridable.

You may be able to add attached properties and process child elements defining those properties, too. For example, if you wanted a hyperlink container to allow different child elements to provide different links, you could subclass from Hyperlink, define a new Link attached property, and handle the click events for the children differently than Hyperlink itself.

You might also be able to achieve some success with the InlineUIContainer and BlockUIContainer elements, which let you embed any UIElement inside it, including custom UIElements.

Kevin Hsu
  • 1,726
  • 11
  • 14
  • I'm afraid attached properties will not help me - I'm trying to **influence layout/rendering behavior of text**. And that text will behave as text in document should only if it is not in any block (so no `InlineUIContainer` with custom `UIElements` or `BlockUIContainer` are right solution). Especially text selection will consider the whole `InlineUIContainer` or `BlockUIContainer` to be one element. For details see my question [Create guitar chords editor in WPF](http://stackoverflow.com/questions/5796457/create-guitar-chords-editor-in-wpf-from-richtextbox) and questions linked there. – Rasto Apr 29 '11 at 20:48
  • 1
    I think you may just be out of luck on this. If you do find a solution, please do share. – Kevin Hsu Apr 29 '11 at 22:08
  • I might... WPF guys who designed `FlowDocument` seems to forgot about guitarists :(. There is almost always solution with programing languages like C#. It just might be uneasy one - like rewriting half of WPF. This time it looks like I'll have to write something my own `FlowDocument` and `RichTextBox` to some extend :((( – Rasto Apr 29 '11 at 22:28
  • 2
    @drasto Kevin is right on this. You can't change rendering of any TextElement, you can only subclass to change properties. you should mark his answer as accepted. – Markus Hütter May 01 '11 at 12:23
  • @Markus Hütter: After your last 2 answers I already consider you an be authority when it comes to `FlowDocument`. So I'll probably accept Kevins answer after some time (month or so). For now it only gets +1 from me. I was already suspecting that he was right... – Rasto May 02 '11 at 18:57