[英]How can I programmatically access a specific Run after adding it to a Windows.UI.Xaml.Controls.TextBlock?
I want to be able to programmatically access a specific run after adding it to a Windows.UI.Xaml.Controls.TextBlock but I cannot set the Name of a Run because the Name property is read only (allows get but not set).我希望能够在将特定运行添加到 Windows.UI.Xaml.Controls.TextBlock 后以编程方式访问它,但我无法设置运行的名称,因为名称属性是只读的(允许获取但不能设置)。
Each run will be created dynamically through a loop (not at design time (xaml)), and I will not know the index (i-th) as they may be created in any order.每次运行都将通过循环动态创建(而不是在设计时 (xaml)),并且我不知道索引(第 i 个),因为它们可能以任何顺序创建。 I would know a word or keyword that would be in a run, but otherwise I have no other way to identify one individual run from another.
我会知道跑步中的一个词或关键字,但除此之外我没有其他方法可以识别一个人与另一个人的跑步。
How can I programmatically find and access a specific Run that was added to a TextBlock, when I click a button?单击按钮时,如何以编程方式查找和访问添加到 TextBlock 的特定运行?
using Windows.UI.Xaml.Controls;
var myTextBlock = new TextBlock();
var someRun = new Run
{
Text = "Some run to start"
};
myTextBlock.Inlines.Add(someRun)
var aRandomRun = new Run
{
Text = "A random run with some words"
};
myTextBlock.Inlines.Add(aRandomRun)
var anotherRun = new Run
{
Text = "Another run, could be anything"
};
myTextBlock.Inlines.Add(anotherRun)
private void GetTextButton_Click(object sender, RoutedEventArgs e)
{
// Get the text of any of these above runs in myTextBlock
// I tried "FindName" but since I cannot set the the name of the Run, I cannot use FindName to find the Run
var anotherRunText = myTextBlock.Inlines.Where(x=> x.FindName
}
If your only way of distinguishing the Run
's from each other is by their text.如果您区分
Run
的唯一方法是通过它们的文本。 You could indeed use what you have tried already, however instead of using the Name
property you use the text property:您确实可以使用您已经尝试过的方法,但是您不使用
Name
属性,而是使用 text 属性:
string FindInlineText(string keyword)
{
string anotherRunText = ((Run)myTextBlock.Inlines.First(i => ((Run)i).Text.Contains(keyword))).Text;
return anotherRunText;
}
It does not seem possible to approach this in another way, since Run.Name
is get only in code behind, and although the Tag
property should be inherited from the FrameworkContentElement
class it is not available to set.似乎不可能以另一种方式解决这个问题,因为
Run.Name
仅在代码后面获取,并且尽管Tag
属性应该从FrameworkContentElement
class 继承,但它不可用于设置。
If you do need to use another identifier to distinguish the text elements from each other, you may look into using a horizontal StackPanel
and multiple TextBlock
instead:如果您确实需要使用另一个标识符来区分文本元素,您可以考虑使用水平
StackPanel
和多个TextBlock
:
StackPanel stack = new StackPanel { Orientation = Windows.UI.Xaml.Controls.Orientation.Horizontal };
for (int i = 0; i < 10; i++)
{
stack.Children.Add(new TextBlock
{
Name = $"inlineblock_{i}",
Text = i.ToString()
});
}
And now you are ofcourse able to get the text of the correct TextBlock
by using:现在您当然可以使用以下方法获取正确
TextBlock
的文本:
string certainText = ((TextBlock)stack.Children.First(i => ((TextBlock)i).Name == certainName)).Text;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.