[WPF] TextBlock小范例
2010-09-07 17:17
218 查看
using System; using System.Windows; using System.Windows.Controls; using System.Windows.Input; using System.Windows.Media; using System.Windows.Documents; namespace Cloud.FormatTheText { public class FormatTheText : Window { [STAThread] public static void Main() { Application app = new Application(); app.Run(new FormatTheText()); } public FormatTheText() { this.Title = "Cloud Orz"; TextBlock txt = new TextBlock(); txt.FontSize = 32; Run run = new Run("This is some "); run.MouseDown += new MouseButtonEventHandler(run_MouseDown); run.Foreground = Brushes.Red; txt.Inlines.Add(run); txt.Inlines.Add(new Italic(new Run("italic"))); txt.Inlines.Add(" text, and this is some "); txt.Inlines.Add(new Bold(new Run("bold"))); txt.Inlines.Add(" text, and this is some "); txt.Inlines.Add(new Bold(new Italic(new Run("bold italic")))); txt.Inlines.Add(" text."); txt.TextWrapping = TextWrapping.Wrap; this.Content = txt; } void run_MouseDown(object sender, MouseButtonEventArgs e) { Run run = sender as Run; if (e.ChangedButton == MouseButton.Left) run.FontStyle = run.FontStyle == FontStyles.Italic ? FontStyles.Normal : FontStyles.Italic; if (e.ChangedButton == MouseButton.Right) run.FontWeight = run.FontWeight == FontWeights.Bold ? FontWeights.Normal : FontWeights.Bold; } } }
这些代码,就是很单纯的建立一个TextBlock,然后放进Window的Content property
若是直接将string放入Content property, 将会先建立一个TextBlock,以显示出字串
同时示范了,如何安装事件到Run对象中
相关文章推荐
- [WPF系列]-基础 TextBlock
- 潜移默化学会WPF(样式篇)---TextBlock ☞超链接
- WPF显示经常使用的几个显示文字控件TextBox, TextBlock, Lable
- 创建ADO.NET Entity Framework 和 WPF 范例程序(提供范例程序下载)
- 使用微软Kinect进行手势识别操作的一个简单范例(2)(WPF-C#)
- 在WPF中区别TextBlock和Label
- WPF 里面的 Run .感觉这名称真没取好,application 里面有个 run, textblock 里面也有个.
- wpf中textbox与textblock有什么区别
- WPF中的TextBlock
- WPF显示常用的几个显示文字控件TextBox, TextBlock, Lable
- WPF中的TextBlock
- wpf:区别TextBlock和Label
- WPF的textblock实例
- wpf 类似TextBlock外观的Button的样式
- 在WPF中区别TextBlock和Label
- [WPF] 彩虹画刷小范例
- 分享初学者的第一个WPF应用程序——Timer线程时时刷新当前系统时间赋值于相关属性,并将其值绑定于TextBlock的Text属性
- 使用微软Kinect进行手势识别操作的一个简单范例(WPF-C#)
- WPF控件TextBlock中文字自动换行
- wpf,能够复制文字 及自动识别URL超链接的TextBlock