简体   繁体   English

将特定运行添加到 Windows.UI.Xaml.Controls.TextBlock 后,如何以编程方式访问它?

[英]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.

相关问题 C#Visual Studio 2017无法将类型字符串隐式转换为windows.ui.xaml.controls.textblock - C# Visual Studio 2017 cannot implicitly convert type string to windows.ui.xaml.controls.textblock 如何在Windows Phone 7 XAML中检查TextBlock是否包含特定字符串? - How to write check if a TextBlock consists a specific string in windows phone 7 XAML? 以编程方式从Windows Phone 8.1 XAML中的ListView中的特定ListViewItem到达TextBlock - Reach a TextBlock from a specific ListViewItem from the ListView in Windows Phone 8.1 XAML programmatically 如何在没有 XAML 的情况下将换行符插入 TextBlock? - How can I insert a newline into a TextBlock without XAML? Windows.UI.Xaml.Controls.SearchBox在哪里? - Where is Windows.UI.Xaml.Controls.SearchBox? 如何以编程方式编写此XAML? - How can I write this XAML programmatically? XAML如何创建一个TextBlock响应式? - XAML How do I create a TextBlock responsive? 如何以编程方式从App.xaml访问资源字符串? - How can I programmatically access a resource string from App.xaml? 命名空间 Windows.UI.Xaml.Controls 和 Microsoft.UI.Xaml.Controls 之间的区别 - Difference between namespace Windows.UI.Xaml.Controls and Microsoft.UI.Xaml.Controls 如何在XAML中访问ListBox的DataTemplate(但不是Binding)中的TextBlock? - How do I access a TextBlock which is inside my ListBox's DataTemplate (but not Binding) in XAML?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM