1 TextBox简单实例

内容摘要:包含文本的选中,弹出什么类型的键盘,回车隐藏键盘,

<Grid Name="root" Background="Transparent">
<StackPanel Margin="120 0 0 0">
<!--
AcceptsReturn - 是否接受回车符
IsReadOnly - 是否只读
SelectedText - 选中的文本内容
-->
<TextBox Name="txtDemo" AcceptsReturn="True" TextWrapping="Wrap" MaxLength="50" TextAlignment="Center" HorizontalAlignment="Left" Width="200" Height="100" Text="webabcd" Loaded="txt_Loaded_1" /> <!--用于显示 txt 中被选中的文本内容-->
<TextBox Name="txtReadOnly" IsReadOnly="True" Text="{Binding SelectedText, ElementName=txt}" HorizontalAlignment="Left" Width="200" Height="100" Margin="0 10 0 0" /> <!--
InputScope - 指定 SIP(Soft Input Panel)的类型
-->
<TextBox InputScope="Number" FlowDirection="RightToLeft" HorizontalAlignment="Left" Margin="0 10 0 0" /> <!--
后台设置此 TextBox 的 InputScope
-->
<TextBox Name="txtInputScope" HorizontalAlignment="Left" Margin="0 10 0 0" KeyDown="txtInputScope_KeyDown_1"/>
</StackPanel>
</Grid>

后台代码:

private void txt_Loaded_1(object sender, RoutedEventArgs e)
{
// 让 txtDemo 获取焦点
txtDemo.Focus(Windows.UI.Xaml.FocusState.Programmatic);
// 将 txtDemo 中的文本从第 3 个字符开始的一共 4 个字符设置为选中状态
txtDemo.Select(, ); /*
* 与文本操作相关的属性和方法还有: SelectionStart, SelectionLength, SelectedText, SelectAll(), Select(int start, int length), GetRectFromCharacterIndex(int charIndex, bool trailingEdge)
*/
} void TextBoxDemo_Loaded(object sender, RoutedEventArgs e)
{
// 设置 txtInputScope 的 InputScope
InputScope scope = new InputScope();
InputScopeName name = new InputScopeName(); name.NameValue = InputScopeNameValue.ChineseFullWidth;
scope.Names.Add(name); txtInputScope.InputScope = scope;
} private void txtInputScope_KeyDown_1(object sender, Windows.UI.Xaml.Input.KeyRoutedEventArgs e)
{
// 判断用户是否按下了 SIP 上的回车键
if (e.Key == VirtualKey.Enter)
{
// 转移焦点,虚拟键盘会自动隐藏
txtReadOnly.Focus(FocusState.Programmatic);
}
}

2、密码框

<!--
Password - 密码值
PasswordChar - 密码框所显示显示的密码替代字符。默认值为“●”
IsPasswordRevealButtonEnabled - 是否显示“显示密码明文”按钮
-->
<PasswordBox Name="pwd" Width="200" HorizontalAlignment="Left" MaxLength="16" IsPasswordRevealButtonEnabled="True" />

3、RichEditBox富文本编辑框

内容摘要:富文本框支持文本颜色,文本加粗,文本斜体,还有文本搜索,高亮显示等。

<Grid Name="root" Background="Transparent">
<StackPanel>
<StackPanel Orientation="Vertical">
<Button Name="btnBold" Content="加粗" Click="btnBold_Click_1" Margin="0 0 10 0" />
<Button Name="btnItalic" Content="斜体" Click="btnItalic_Click_1" Margin="0 0 10 0" />
<TextBox x:Name="txtSearch" Margin="0,0,164,0"/>
<Button Name="btnSearch" Content="搜索" Click="btnSearch_Click_1" />
</StackPanel>
<RichEditBox x:Name="txtEditor" Width="239" Height="240" HorizontalAlignment="Left" Margin="0 10 0 0" />
</StackPanel>
</Grid>
private void btnBold_Click_1(object sender, RoutedEventArgs e)
{
// 获取选中的文本
ITextSelection selectedText = txtEditor.Document.Selection;
if (selectedText != null)
{
// 实体化一个 ITextCharacterFormat,指定字符格式为加粗
ITextCharacterFormat charFormatting = selectedText.CharacterFormat;
charFormatting.Bold = FormatEffect.Toggle; // 设置选中文本的字符格式
selectedText.CharacterFormat = charFormatting;
}
} private void btnItalic_Click_1(object sender, RoutedEventArgs e)
{
// 获取选中的文本
ITextSelection selectedText = txtEditor.Document.Selection;
if (selectedText != null)
{
// 实体化一个 ITextCharacterFormat,指定字符格式为斜体
ITextCharacterFormat charFormatting = selectedText.CharacterFormat;
// charFormatting.FontStyle = (FontStyle)Enum.Parse(typeof(FontStyle), "Italic", true);
charFormatting.Italic = FormatEffect.Toggle; // 设置选中文本的字符格式
selectedText.CharacterFormat = charFormatting;
} }
// 保存已经被高亮的 ITextRange
List<ITextRange> _highlightedWords = new List<ITextRange>();
// 高亮显示用户搜索的字符 private void btnSearch_Click_1(object sender, RoutedEventArgs e)
{
// 清除高亮字符的高亮效果
ITextCharacterFormat charFormat;
for (int i = ; i < _highlightedWords.Count; i++)
{
charFormat = _highlightedWords[i].CharacterFormat;
charFormat.BackgroundColor = Colors.Transparent;
_highlightedWords[i].CharacterFormat = charFormat;
}
_highlightedWords.Clear(); // 获取全部文本,并将操作点移动到文本的起点
ITextRange searchRange = txtEditor.Document.GetRange(, TextConstants.MaxUnitCount);
searchRange.Move(, ); bool textFound = true;
do
{
// 在全部文本中搜索指定的字符串
if (searchRange.FindText(txtSearch.Text, TextConstants.MaxUnitCount, FindOptions.None) < )
{
textFound = false;
}
else
{
_highlightedWords.Add(searchRange.GetClone()); // 实体化一个 ITextCharacterFormat,指定字符背景颜色为黄色
ITextCharacterFormat charFormatting = searchRange.CharacterFormat;
charFormatting.BackgroundColor = Colors.Yellow; // 设置指定文本的字符格式(高亮效果)
searchRange.CharacterFormat = charFormatting;
}
} while (textFound); }
}

4、RichTextBlock 富文本显示控件

摘要:可以在文本内部嵌入各种控件

<StackPanel >
<!--
RichTextBlock - 用于显示富文本的控件
Blocks - 富文本的内容
Paragraph - 每一个 Paragraph 代表一段内容,其继承自 Block
Inlines - 每个 Paragraph 下都有一个内联元素集合,其用法与 TextBlock 的 Inlines 基本相同,不同之处在于只有在 RichTextBlock 中才能使用 InlineUIContainer
TextIndent - 指定此段文本的首行的缩进量 注:其他 n 多属性基本同 TextBlock
-->
<RichTextBlock FontSize="14.667" HorizontalAlignment="Left">
<RichTextBlock.Blocks>
<Paragraph TextIndent="20">
Windows Phone 8采用和Windows 8相同的针对移动平台精简优化
NT内核并且内置诺基亚地图,这标志着移动版Windows Phone将提前
与Windows系统同步,部分Windows8应用可以更方便的移植到手机
</Paragraph>
<Paragraph>
<InlineUIContainer>
<StackPanel HorizontalAlignment="Left">
<TextBlock Text="显示一张图片" />
<Image Source="/Assets/Logo.png" Width="100" Height="100" />
</StackPanel>
</InlineUIContainer>
</Paragraph>
</RichTextBlock.Blocks>
</RichTextBlock>
</StackPanel>

5、RichTextBlockOverflow 处理富文本框溢出

<StackPanel  Orientation="Vertical">

<RichTextBlock HorizontalAlignment="Left" Width="390" Height="100" OverflowContentTarget="{Binding ElementName=txtOverflow}">
<Paragraph>
一打开Windows 8计算机,用户就会发现明显的变化。呈现在用户眼前的不再是熟悉的桌面,而是由漂亮、现代化的瓷贴(tile)以及最适合在触控屏上运行的全屏应用构成的环境。这就是“开始”屏,它取代了Windows用户熟悉的“开始”菜单。开始屏不只是菜单,而是占据整个显示屏的一个完整的计算环境,有自己独立的应用和控件。用户仍然可以使用过去的桌面和老式软件,在Windows 8中,桌面就像另外一款应用,用户可以通过点击开始屏上的图标或按钮运行桌面。
这是一个大胆的举措,基于瓷贴的环境非常好,将受到用户的欢迎。这一环境让人感觉很自然,特别是在触控屏设备上,使Windows进入了平板电脑时代。它可能甚至标志着一个漫长过渡期的开始,新设计将逐步取代原来的设计,当然,这取决于微软吸引新型应用的速度。
Windows将提供两种完全不同的用户体验。微软的目的是提供一款既能在传统PC,也能在平板电脑上运行的操作系统,包括采用触控方式操作的天气应用和采用鼠标操作的Excel都能在Windows 8中运行。这一策略完全不同于苹果,苹果的iPad平板电脑和Mac计算机运行不同的操作系统。
双环境策略可能会让传统PC用户感到困惑。新、旧两种环境都可以通过触控、鼠标、键盘进行操作,但触控更适合新环境,鼠标、键盘更适合旧环境。
Windows 8将带有两种不同版本的IE,许多功能也不相同。例如,新型应用通常缺乏传统应用中的标准菜单、工具条、改变尺寸和关闭按钮。微软坚信用户困惑是暂时的,将被运行Office等传统办公软件的能力所抵消。Office不能在iPad或Android平板电脑上运行。
Windows 8可能给用户带来更多困惑。Windows 8有两种版本:一个版本面向标准的X86 PC,另一款版本――Windows RT面向配置ARM架构芯片的设备。
当然,两种版本之间的区别很大。在X86设备上,用户可以运行新型应用,也可以通过桌面环境运行传统的Windows应用。但是,用户不能在ARM设备上安装和运行传统Windows应用。能同时在X86和ARM设备上运行的唯一一款主要软件是一款新版Office,但ARM版Office不包含Outlook。用户可以通过网络商店下载所有新型应用。
微软将首次推出自主品牌平板电脑Surface,与传统硬件合作伙伴推出的Windows 8和Windows RT平板电脑竞争。
</Paragraph>
</RichTextBlock>
<RichTextBlockOverflow x:Name="txtOverflow" Height="100" OverflowContentTarget="{Binding ElementName=txtOverflow2}" /> <!--
RichTextBlock - 富文本显示框
OverflowContentTarget - 当 RichTextBlock 中的内容溢出时,将溢出文字绑定到指定的 RichTextBlockOverflow 中 RichTextBlockOverflow - 用于显示 RichTextBlock 或其他 RichTextBlockOverflow 中的溢出文字
OverflowContentTarget - 当此 RichTextBlockOverflow 中的内容也溢出时,将溢出文字绑定到指定的其他 RichTextBlockOverflow 中
HasOverflowContent - 是否有溢出内容可显示
ContentSource - 源内容,返回对应的 RichTextBlock
-->
<RichTextBlockOverflow Name="txtOverflow2" Width="390" Height="100" HorizontalAlignment="Left" OverflowContentTarget="{Binding ElementName=txtOverflow3}"/> <RichTextBlockOverflow Name="txtOverflow3" Height="100" Margin="0,0,29,0" />
</StackPanel>

WinStore控件之TextBox的更多相关文章

  1. 重新想象 Windows 8 Store Apps (1) - 控件之文本控件: TextBlock, TextBox, PasswordBox, RichEditBox, RichTextBlock, RichTextBlockOverflow

    原文:重新想象 Windows 8 Store Apps (1) - 控件之文本控件: TextBlock, TextBox, PasswordBox, RichEditBox, RichTextBl ...

  2. 关于继承扩展ASP.NET控件(以Textbox为例)

    以下是一个相对简陋的扩展, 主要是针对金额显示的Textbox扩展. using System; using System.Collections.Generic; using System.Linq ...

  3. winform学习(7)Label控件、Button控件、TextBox控件

    Label控件是System.Windows.Forms.Label 类提供的控件. 作用:主要用来提供其他控件的描述文字,例如:登录窗体上的用户名.密码(输入框前面的字) Button控件是Syst ...

  4. WinStore控件之Button、HyperlinkButton、RadioButton、CheckBox、progressBar、ScrollViewer、Slider

    1.Button protected override void OnNavigatedTo(NavigationEventArgs e) { /* * Button - 按钮控件,其全部功能是通过其 ...

  5. WinStore控件之TextBlock

    1  TextBlock简单实例应用 <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}&quo ...

  6. WinStore控件之Button

    1 Buton入门简单应用 <StackPanel > <Button Content="按钮1" Height="80" Name=&quo ...

  7. 【Win 10应用开发】提供建议列表的输入控件(AutoSuggestBox)

    AutoSuggestBox控件与TextBox控件相似,但,AutoSuggestBox控件可以提供一个下拉列表,用户可以从弹出的下拉列表中选择一个项,并把被选项的内容显示在输入框上.就类似于搜索引 ...

  8. 【Win 10应用开发】使用RichEditBox控件应注意的问题

    RichEditBox控件支持对多格式文本进行编辑,一般的文本输入控件可以使用TextBox,不过,如果希望编辑格式较为复杂的文本,就可以考虚使用RichEditBox控件. RichEditBox控 ...

  9. [WinForm]WinForm跨线程UI操作常用控件类大全

    前言 在C#开发的WinForm窗体程序开发的时候,经常会使用多线程处理一些比较耗时之类的操作.不过会有一个问题:就是涉及到跨线程操作UI元素. 相信才开始接触的人一定会遇上这个问题. 为了解决这个问 ...

随机推荐

  1. Lambda应用设计模式

    前言 在使用 Lambda 表达式时,我们常会碰到一些典型的应用场景,而从常用场景中抽取出来的应用方式可以描述为应用模式.这些模式可能不全是新的模式,有的参考自 JavaScript 的设计模式,但至 ...

  2. Kali Linux Web 渗透测试视频教—第二十课-利用kali linux光盘或者usb启动盘破解windows密码

    Kali Linux Web 渗透测试视频教—第二十课-利用kali linux光盘或者usb启动盘破解windows密码 文/玄魂 目录 Kali Linux Web 渗透测试视频教—第二十课-利用 ...

  3. 团队项目——站立会议 DAY1

    团队项目--站立会议 DAY1        团队成员介绍(5人):张靖颜.何玥.钟灵毓秀.赵莹.王梓萱        今日(2016/5/6)为站立会议的第一天,一起对团队项目进行讨论,并对每个人的 ...

  4. [游戏模版5] Win32 折线 弧线

    >_<:first build some points put in poly1[],poly2[] and poly3[] in the function of InitInstance ...

  5. 在Html中使用Requirejs进行模块化开发

    在前端模块化的时候,不仅仅是js需要进行模块化管理,html有时候也需要模块化管理.这里就介绍下如何通过requirejs,实现html代码的模块化开发. 如何使用requirejs加载html Re ...

  6. JQuery官方学习资料(译):使用JQuery的.index()方法

        .index()是一个JQuery对象方法,一般用于搜索JQuery对象上一个给定的元素.该方法有四种不同的函数签名,接下来将讲解这四种函数签名的具体用法. 无参数的.index() < ...

  7. FIR.im Weekly - 上周微博热转资源精选

    LeakCanary: 让内存泄露无所遁形 Square 开源的 LeakCanary,国内开发者 @廖祜秋liaohuqiu 翻译了对应的官方博客,撰写了中文使用说明文档,同时还写了一个小 Demo ...

  8. android: 接收和发送短信

    8.2    接收和发送短信 收发短信应该是每个手机最基本的功能之一了,即使是许多年前的老手机也都会具备这 项功能,而 Android 作为出色的智能手机操作系统,自然也少不了在这方面的支持.每个 A ...

  9. 如何编写一个PHP的C扩展

    为什么要用C扩展 C是静态编译的,执行效率比PHP代码高很多.同样的运算代码,使用C来开发,性能会比PHP要提升数百倍.IO操作如CURL,因为耗时主要在IOWait上,C扩展没有明显优势. 另外C扩 ...

  10. HOWTO - Basic MSI安装包在安装运行过程中如何获取完整源路径

    有朋友问到如何在一个Windows Installer安装包中获取安装包源路径,就是在安装包运行过程中动态获取*.msi所在完整路径. 这个问题分两类,如果我们的安装包只是一个*.msi安装文件,那么 ...