有没有办法从文本块中显示文本?

时间:2014-08-03 12:46:49

标签: c# wpf

我有一个继承自textblock控件的自定义控件,我在很少的条件下对设计文本进行了一些操作,并通过flowdirection和textalignment对齐它。

我的问题 - 是否有办法获取显示的文字?因为源文本没有改变 - 显示改变了..

例如:

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

将显示:!简单测试

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

将显示:      简单测试!

我想在后面的代码中显示displaye文本。 对于第一个例子,我希望得到:!简单的测试 对于第二个例子,我希望得到:简单的测试! 有可能吗?

2 个答案:

答案 0 :(得分:1)

您必须设置名称属性:

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

然后你可以在后面的代码中调用它:

this.SimpleTextBlock.Text

请参阅:

<强>正文块

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

答案 1 :(得分:0)

我找到答案 - 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);
        }
    }
}
}