How can I create subclass of class Inline ? (the o

2020-07-10 11:56发布

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.

1条回答
The star\"
2楼-- · 2020-07-10 12:20

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.

查看更多
登录 后发表回答