【问题标题】:Is there a way to getting displayed text from textblock?有没有办法从文本块中获取显示的文本?
【发布时间】:2014-09-26 02:33:11
【问题描述】:

我有一个继承自 textblock 控件的自定义控件,我做了一些操作以通过几个条件设计文本,并通过 flowdirection 和 textalignment 对齐它。

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

例如:

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

将显示:!简单测试

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

将显示: 简单测试!

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

【问题讨论】:

  • 可能我的问题不太清楚,我会附上问题的例子。
  • 唯一的区别是文本的显示方式。在任何情况下,你都持有一个字符串,它的最后一个字符是!。你想做什么?
  • 源文本保持不变,显示的文本被流向和文本对齐改变,这就是我想要的。

标签: c# wpf


【解决方案1】:

您必须设置 Name 属性:

<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

【讨论】:

  • 获取文本不是问题 - 再读一遍,我想获取显示文本(不是文本本身)..
  • 我明白了,我没有正确理解你。这可能是一个问题,因为在绘制元素时可能会完成转换,因此不会有返回字符串的属性或方法。
  • 为什么需要获取输出字符串?你的目标是什么?使用已经在 UI 中呈现给用户的字符串通常不是一个好主意。如果你真的需要它,那么我会尝试 GetValue 或 GetVisualChild 之类的方法,或者遍历可视化树以获得 TextBlock 的可视化元素,但我不确定这是否真的对你有帮助。如果你没有正确理解它也不是那么容易。
  • 这是因为我有一个服务器为我提供了很多用希伯来语编写的字符串,所以我需要对它们进行操作以便在 UI (wpf) 上正确设计 - 因为这些字符串有很多情况那应该通过 flowdirection 或 textalignment 或两者来改变方向,所以我想聚合好的显示字符串并将它们转发到其他系统。
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多