0

I have a custom control that inherited from textblock control, i doing a little manipulate for design the text by few conditions, and align it by flowdirection and textalignment.

My question - is there a way to getting the displayed text? because the sourced text is not changed - the display is changed..

For example:

<TextBlock Text="Simple Test!" FlowDirection="RightToLeft" TextAlignment="Left"/>

will display: !Simple Test

<TextBlock Text="Simple Test!" FlowDirection="LeftToRight" TextAlignment="Right"/>

will display: Simple Test!

and i want to getting the displaye text in code behind.. for first example i expect to getting: !Simple Test and for second example i expect to getting: Simple Test! It's possible?

David Michaeli
  • 367
  • 5
  • 26
  • maybe my question is not clearly, i will attach an example to the question. – David Michaeli Aug 03 '14 at 12:52
  • The only difference is the way the text is displayed. in any case you hold a string wich its last character is `!`. What do you want to do? – dovid Aug 03 '14 at 13:03
  • the sourced text is remains same and the displayed text is changed by the flowdirection and the textalignment, this is what i want to get. – David Michaeli Aug 03 '14 at 13:09
  • possible duplicate of [Get Displayed Text from TextBlock](http://stackoverflow.com/questions/4319031/get-displayed-text-from-textblock) – kjbartel Aug 04 '14 at 09:15

2 Answers2

1

You have to set the Name property:

<TextBlock Name="SimpleTextBlock" Text="Simple Test!" FlowDirection="RightToLeft" TextAlignment="Left"/>

And then you can call it like this in code behind:

this.SimpleTextBlock.Text

See:

Textblock

http://msdn.microsoft.com/en-us/library/system.windows.controls.textblock(v=vs.110).aspx

Textblock.Text

http://msdn.microsoft.com/en-us/library/system.windows.controls.textblock.text(v=vs.110).aspx

Santhos
  • 3,348
  • 5
  • 30
  • 48
  • to getting the text isn't the problem - read again, i want to getting the display text (no the text himself).. – David Michaeli Aug 03 '14 at 13:08
  • I see, I did not understand you properly. It might be a problem since the transformation might be done when drawing the element, thus there would be no property or method returning the string. – Santhos Aug 03 '14 at 14:57
  • Why do you need to get the output string? What is your goal? It is usually not a good idea to work with strings that have already been presented to the user in the UI. In case you really need it, then I would try methods like GetValue or GetVisualChild or traverse the visual tree to obtain the visual element of the TextBlock, but I am not sure if that would really help you. It also isn't that easy if you don't understand it properly. – Santhos Aug 03 '14 at 15:03
  • It's because i have a server that supply me a lot of strings that written in hebrew, so i need to manipulate on them for design properly on UI (wpf) - for those strings have a lot of case that should to change the direction by flowdirection or textalignment or both, so i want to aggrgate the good displayied strings and forward them to other systems. – David Michaeli Aug 03 '14 at 15:42
0

I finding the answer - Get Displayed Text from TextBlock.

 private string GetTextFromVisual(Visual v)
{
Drawing textBlockDrawing = VisualTreeHelper.GetDrawing(v);
var glyphs = new List<PositionedGlyphs>();

WalkDrawingForGlyphRuns(glyphs, Transform.Identity, textBlockDrawing);

// Round vertical position, to provide some tolerance for rounding errors
// in position calculation. Not totally robust - would be better to
// identify lines, but that would complicate the example...
var glyphsOrderedByPosition = from glyph in glyphs
                                let roundedBaselineY = Math.Round(glyph.Position.Y, 1)
                                orderby roundedBaselineY ascending, glyph.Position.X ascending
                                select new string(glyph.Glyphs.GlyphRun.Characters.ToArray());

return string.Concat(glyphsOrderedByPosition);
}

[DebuggerDisplay("{Position}")]
public struct PositionedGlyphs
{
public PositionedGlyphs(Point position, GlyphRunDrawing grd)
{
    this.Position = position;
    this.Glyphs = grd;
}
public readonly Point Position;
public readonly GlyphRunDrawing Glyphs;
}

private static void WalkDrawingForGlyphRuns(List<PositionedGlyphs> glyphList, Transform tx, Drawing d)
{
    var glyphs = d as GlyphRunDrawing;
if (glyphs != null)
{
    var textOrigin = glyphs.GlyphRun.BaselineOrigin;
    Point glyphPosition = tx.Transform(textOrigin);
    glyphList.Add(new PositionedGlyphs(glyphPosition, glyphs));
}
else
{
    var g = d as DrawingGroup;
    if (g != null)
    {
        // Drawing groups are allowed to transform their children, so we need to
        // keep a running accumulated transform for where we are in the tree.
        Matrix current = tx.Value;
        if (g.Transform != null)
        {
            // Note, Matrix is a struct, so this modifies our local copy without
            // affecting the one in the 'tx' Transforms.
            current.Append(g.Transform.Value);
        }
        var accumulatedTransform = new MatrixTransform(current);
        foreach (Drawing child in g.Children)
        {
            WalkDrawingForGlyphRuns(glyphList, accumulatedTransform, child);
        }
    }
}
}
Community
  • 1
  • 1
David Michaeli
  • 367
  • 5
  • 26