[UWP] [C#]如何取消选择代码中文本块中的文本 [英] [UWP][C#]How to deselect text in a textblock in code

查看:67
本文介绍了[UWP] [C#]如何取消选择代码中文本块中的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下XAML:

 

< StackPanel>

< TextBlock x:Name = " TextBlock1" />

< TextBlock x:Name =" TextBlock2" />

< / StackPanel>

在PointerPressed事件中,我在代码中调用以下内容:

 

private void TextBlock1_PointerPressed(object sender,PointerRoutedEventArgs e)
{
TextBlock1.SelectAll();

//TextBlock2.DeSelectAll(); //没有可用于取消选择其他文本块的方法

}

不幸的是,没有任何反应,即文本未突出显示,如果IsTextSelectionEnabled已启用,然后光标变为I栏,用户可以进入文本块并选择文本块内的文本,这不是我正在寻找的行为
。 我只是想向用户表明已选择了TextBlock。 也许像我在ListBox控件中选择TextBlock时看到的边框。


另外,需要取消选择其他TextBlock中的文本,并且没有DeSelectAll()或类似的东西,即UnSelectAll()或其他,并且没有Background属性可用于模拟文本选择。 另外,TextBlock.Select()方法
需要TextPointer对象,但TextPointer类没有构造函数,MSDN文档也没有提供任何关于如何获取TextPointer对象的线索。 它看起来像TextPointer对象可以从TextBlock.ContentStart
和TextBlock.ContentEnd属性获得,但 TextPointer属性看起来是只读的。







Steven Edward Wood计算机工程师







$

解决方案


>>如果启用了IsTextSelectionEnabled,则光标变为I栏,用户可以进入文本块并选择文本块内的文本,这不是我正在寻找的行为。


此行为选择了该文本方式是预期的。 


>>我只是想向用户表明已选择了TextBlock。 也许是我看到的边框在ListBox控件中选择TextBlock时。


所以你不是在寻找一个行为如果选择了文本,您将找到一种方法可以告诉客户TextBlock是可选的。它在ListBox控件中显示的行为实际上是ListBoxItem的选定行为,其中
是TextBlock的容器。


现在,如果要实现所选行为, ListBoxItem。您可以创建一个UserControl,将TextBlock放入其中。然后定义一个bool值来指示选择状态。在
UserControl的点击事件中更改UserControl的背景颜色。


>> 此外,TextBlock.Select()方法需要TextPointer对象,但TextPointer类没有构造函数,MSDN文档也没有提供任何关于如何获取TextPointer对象的线索。 


获取方法中使用的TextPointer并不困难。 TextBlock有一个名为  ContentStart 的属性,它是
代表TextBlock中文本内容的开头。


所以当你需要使用它时,你可以像这样调用它:

 private void TextBlock1_PointerExited(object sender,PointerRoutedEventArgs e)
{
TextBlock1.Select(TextBlock1.ContentStart,TextBlock1.ContentStart);
}


祝你好运,


罗伊


I am using the following XAML:

<StackPanel>

<TextBlock x:Name="TextBlock1"/>

<TextBlock x:Name="TextBlock2"/>

</StackPanel>

In the PointerPressed event I call the following in Code:

private void TextBlock1_PointerPressed(object sender, PointerRoutedEventArgs e) { TextBlock1.SelectAll();

//TextBlock2.DeSelectAll(); //no method available to deselect the other textblock

}

Unfortunately, nothing happens i.e. the text is not highlighted, and if the IsTextSelectionEnabled is enabled then the cursor changes to an I bar and the user is able to enter into the textblock and select text inside the textblock which is not the behavior I'm looking for.  I simply want to indicate to the user that the TextBlock is selected.  Perhaps a border like what I see when selecting a TextBlock in a ListBox control.

Also,need to deselect the text in the other TextBlock and there is no DeSelectAll() or anything like that i.e. UnSelectAll() or whatever, and there is no Background property with which I can simulate text selections.  Also, the TextBlock.Select() method requires TextPointer objects but there is no Constructor for the TextPointer class and the MSDN documentation does not provide any clue as to how to get a TextPointer object.  It does look like TextPointer objects can be obtained from TextBlock.ContentStart and TextBlock.ContentEnd properties, but TextPointer properties appear to be readonly.



Steven Edward Wood Computer Engineer


解决方案

Hi,

>>if the IsTextSelectionEnabled is enabled then the cursor changes to an I bar and the user is able to enter into the textblock and select text inside the textblock which is not the behavior I'm looking for.

This behavior that the text is selected in that way is expected. 

>>I simply want to indicate to the user that the TextBlock is selected. Perhaps a border like what I see when selecting a TextBlock in a ListBox control.

So you are not looking for a behavior that the text is selected, you are finding a way that could tell the customers that the TextBlock is selectable. The behavior it shows in the ListBox control is actually the selected behavior of the ListBoxItem which is the container of the TextBlock.

Now, if you want to achieve the selected behavior like the ListBoxItem. You could create a UserControl, put the TextBlock in it. Then defined a bool value to indicate the select status. Change the background color of the UserControl in the tap event of the UserControl.

>> Also, the TextBlock.Select() method requires TextPointer objects but there is no Constructor for the TextPointer class and the MSDN documentation does not provide any clue as to how to get a TextPointer object. 

This is not difficult to get a TextPointer that used in the method. TextBlock has a property called ContentStart,it represents the start of text content in the TextBlock.

So when you need to use it, you could call it like this:

  private void TextBlock1_PointerExited(object sender, PointerRoutedEventArgs e)
        {
            TextBlock1.Select(TextBlock1.ContentStart,TextBlock1.ContentStart);
        }

Best regards,

Roy


这篇关于[UWP] [C#]如何取消选择代码中文本块中的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆