Frage

After converting a SVG to XAML I got a lot of textblocks looking like this :

<TextBlock xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" FontSize="10"  Foreground="#FF000000"Name="text324947"><Span  FontSize="10">79</Span></TextBlock>

Now the question is : how can I access the Text inside the span ? The Text Property of my TextBlock is Empty.

Thanks

War es hilfreich?

Lösung

Use the Inlines property (see link).

Given your XAML, you may do this:

TextBlock tb = this.txt
Span span = (Span) tb.Inlines.FirstInline;
Run run = (Run) span.Inlines.FirstInline;
string text = run.Text;

Andere Tipps

Span is used to group other inline flow content elements together so no property such as Text or Content is directly exposed

here how you can get the text inside the span inline element

<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <TextBlock Name="txtBlock">  <Span Name="span">test</Span> </TextBlock>
    </Grid>
</Window>

    public MainWindow()
    {
        InitializeComponent();
         string s= (span.Inlines.ToArray()[0] as Run).Text;
       // or just try this 
         string s2= ((txtBlock.Inlines.FirstInline as Span).Inlines.FirstInline as Run).Text;  
    }
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top