C# 有没有办法从textblock获取显示的文本?

C# 有没有办法从textblock获取显示的文本?,c#,wpf,C#,Wpf,我有一个从textblock控件继承的自定义控件,我通过一些条件来设计文本,并通过flowdirection和textalignment来对齐它 我的问题-有没有办法获取显示的文本?因为源文本未更改-显示已更改 例如: <TextBlock Text="Simple Test!" FlowDirection="RightToLeft" TextAlignment="Left"/> 将显示:!简单测试 <TextBlock Text="Simple Test!" FlowD

我有一个从textblock控件继承的自定义控件,我通过一些条件来设计文本,并通过flowdirection和textalignment来对齐它

我的问题-有没有办法获取显示的文本?因为源文本未更改-显示已更改

例如:

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

将显示:!简单测试

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

将显示: 简单的测试

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

您必须设置Name属性:

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

Textblock

Textblock.Text

我找到了答案-

私有字符串GetTextFromVisual(Visual v)
{
绘图文本BlockDrawing=VisualTreeHelper.GetDrawing(v);
var glyphs=新列表();
WalkDrawingForglyphuns(字形、变换、标识、文本块绘制);
//圆形垂直位置,为圆形误差提供一定公差
//在位计算。不完全可靠-最好
//确定行,但这会使示例复杂化。。。
var glyphsOrderedByPosition=来自glyphs中的glyph
设roundedBaselineY=Math.Round(glyph.Position.Y,1)
orderby roundedBaselineY升序,glyph.Position.X升序
选择新字符串(glyph.Glyphs.GlyphRun.Characters.ToArray());
返回字符串.Concat(glyphsOrderedByPosition);
}
[调试器显示(“{Position}”)]
公共结构
{
公共位置(点位置、图形grd)
{
这个位置=位置;
这个。Glyphs=grd;
}
公共只读点位置;
公共只读图示符图形图示符;
}
私有静态void walkingDrawingForglyphuns(列表图示符列表、变换tx、图形d)
{
变量glyphs=d作为GlyphRunDrawing;
if(glyphs!=null)
{
var textOrigin=glyphs.GlyphRun.BaselineOrigin;
点位置=tx.Transform(textOrigin);
glyphList.Add(新位置edglyphs(glyphosition,glyphs));
}
其他的
{
var g=d作为绘图组;
如果(g!=null)
{
//允许绘图组变换其子对象,因此我们需要
//为我们在树中的位置保持一个运行的累积变换。
矩阵电流=tx.值;
if(g.Transform!=null)
{
//注意,Matrix是一个结构,因此它修改本地副本而不需要
//影响“tx”变换中的一个。
当前追加(g.Transform.Value);
}
var累计转移=新矩阵转移(当前);
foreach(在g.Children中绘制子对象)
{
步进绘图(字形列表、累计转换、子项);
}
}
}
}

也许我的问题不清楚,我会在问题后面附上一个例子。唯一的区别是文本的显示方式。在任何情况下,您持有的字符串的最后一个字符是
。你想做什么?源文本保持不变,显示的文本由流向和文本对齐方式更改,这是我想要得到的。获取文本的可能重复不是问题-再次阅读,我想要获取显示文本(不是文本本身)…我明白了,我没有正确理解你。这可能是一个问题,因为转换可能在绘制元素时完成,因此没有返回字符串的属性或方法。为什么需要获取输出字符串?你的目标是什么?使用已经在UI中呈现给用户的字符串通常不是一个好主意。如果您真的需要它,那么我会尝试像GetValue或GetVisualChild这样的方法,或者遍历可视化树来获得TextBlock的可视化元素,但我不确定这是否真的对您有帮助。如果你不理解的话,这也不是那么容易。因为我有一个服务器,它为我提供了很多用希伯来语编写的字符串,所以我需要对它们进行操作,以便在UI(wpf)上正确设计-因为这些字符串有很多大小写,应该通过flowdirection或textalignment或两者来更改方向,因此,我想将显示良好的字符串组合起来,并将它们转发给其他系统。
this.SimpleTextBlock.Text
 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);
        }
    }
}
}