1

I am trying to superscript a wpf binding, for example: c#:

public string PolynomialAsString = "10x^2 + 3x - 1";

xaml:

<TextBlock Grid.Row="1" Text="{Binding PolynomialAsString}" />

To be displayed as: 10x² + 3x - 1 in the wpf application.

The only ways I have found to do this online hardcode the position of the superscipt in the xaml like so:

<TextBlock>
    <Run>Normal Text</Run>
    <Run Typography.Variants="Superscript">Superscript Text</Run>
    <Run Typography.Variants="Subscript">Subscript Text</Run>
</TextBlock>

But I would like do this dynamically, as I do not know the positions of the superscripts.

Any help is greatly appreciated

Alfie
  • 297
  • 1
  • 12
  • replace ^ with as mentioned here https://stackoverflow.com/a/41223500/3201354 – Jignesh Oct 10 '18 at 10:25
  • @Jignesh I've tried that, but it doesn't seem to work when it is bound to the `TextBlock`, also that unicode pattern is only consistent for ^2 & ^3 – Alfie Oct 10 '18 at 10:26
  • Try one of these: https://stackoverflow.com/a/9071856/1136211 – Clemens Oct 10 '18 at 10:29
  • You could always derive your own SuperScriptTextBlock which parses and adds the inlines itself when the binding updates. I've done similar for highlighting text. Pain the arse though. Can't believe there isn't a better way. – GazTheDestroyer Oct 10 '18 at 10:33

1 Answers1

0

I found a solution using the wpf-math library.

Here is an example:

'c#':

public string PolynomialAsString = "10x^2 + 3x - 1";

'xaml':

<Window ... xmlns:controls="clr-namespace:WpfMath.Controls;assembly=WpfMath">
    <controls:FormulaControl Grid.Row="1" Formula="{Binding PolynomialAsString}" />
</ Window>
Alfie
  • 297
  • 1
  • 12