来自Window Presentation Foundation Program Design的读书笔记 (第四篇 下)

我们将一个tooltip控件绑定到button上,以便鼠标经过button时,可以显示提示信息,

   1:  private void ToolTipTest()
   2:  {
   3:      Button btn = new Button();
   4:      btn.Content = "Hello world!";
   5:      btn.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
   6:      btn.VerticalAlignment = System.Windows.VerticalAlignment.Center;
   7:      ToolTip tip = new ToolTip();
   8:      btn.ToolTip = tip;
   9:      tip.Content = "This is button";
  10:   
  11:      Content = btn;
  12:  }

当然你可以在tooltip中使用格式化字符串(new Blod(new Run(“Test”)));或者Image来进行解释。


其他继承自ContentControl的控件还有Label:

   1:  Label lab = new Label();
   2:  lab.Content = "This is Label";

Label控件有个特点,就是不会得到焦点,这样可以让焦点专注于与用户输入相关的控件上。

TextBox控件,WPF中支持两种TextBox控件,分别是:

   1:  Control
   2:      TextBoxBase(abstract)
   3:          TextBox
   4:          RichTextBox

虽然TextBox还有内容,但是TextBox并不认为是ContentControl控件,因为内容一定是文字,所以它具有一个Text property。

因为TextBox继承自Control,所以可以指定背景色和前景色,任何时候TextBox都会使用同一种颜色,而RichTextBox控件允许你使用不同的颜色或者格式来格式化文字。

一个例子:

   1:  using System;
   2:  using System.Collections.Generic;
   3:  using System.Linq;
   4:  using System.Text;
   5:  using System.Windows;
   6:  using System.Windows.Controls;
   7:  using System.Windows.Data;
   8:  using System.Windows.Documents;
   9:  using System.Windows.Input;
  10:  using System.Windows.Media;
  11:  using System.Windows.Media.Imaging;
  12:  using System.Windows.Navigation;
  13:  using System.Windows.Shapes;
  14:   
  15:  namespace WPF_ButtonAndControls
  16:  {
  17:      /// <summary>
  18:      /// TextBox.xaml 的交互逻辑
  19:      /// </summary>
  20:      public partial class TextBoxEx : Window
  21:      {
  22:          public TextBoxEx()
  23:          {
  24:              InitializeComponent();
  25:              Loaded += PageOnLoaed;
  26:          }
  27:   
  28:          TextBox tbx = null;
  29:          private void PageOnLoaed(object sender, RoutedEventArgs e)
  30:          {
  31:              FirstTextBox();    
  32:          }
  33:   
  34:          private void FirstTextBox()
  35:          {
  36:              Title = "Text Block";
  37:              SizeToContent = System.Windows.SizeToContent.WidthAndHeight;
  38:              WindowStyle = System.Windows.WindowStyle.ToolWindow;
  39:              WindowStartupLocation = System.Windows.WindowStartupLocation.CenterScreen;
  40:              ShowInTaskbar = false;
  41:   
  42:              tbx = new TextBox();
  43:              tbx.Width = 300;
  44:              tbx.Margin = new Thickness(48);
  45:              Content = tbx;
  46:          }
  47:   
  48:          public string Text
  49:          {
  50:              get { return tbx.Text; }
  51:              set { tbx.Text = value; tbx.SelectionStart = tbx.Text.Length; }
  52:          }
  53:   
  54:          protected override void OnKeyDown(KeyEventArgs e)
  55:          {
  56:              if (e.Key == Key.Enter)
  57:              {
  58:                  Close();
  59:              }
  60:          }
  61:   
  62:   
  63:      }
  64:  }

这是我们的第一个TextBox,最简单的。

 

我们在看一个例子,这个例子需要使用到上面的TextBoxEx这个窗体和一个包含有Frame的窗体:

Frame窗体:

   1:  Frame frm = null;
   2:  private void FrameTest()
   3:  {
   4:      frm = new Frame();
   5:      Content = frm;
   6:  }
   7:   
   8:  protected override void OnKeyDown(KeyEventArgs e)
   9:  {
  10:      if (e.Key == Key.Enter)
  11:      {
  12:          TextBoxEx tbe = new TextBoxEx();
  13:          tbe.Owner = this;
  14:          tbe.ShowDialog();
  15:   
  16:          try
  17:          {
  18:              frm.Source = new Uri(tbe.Text);
  19:          }
  20:          catch (Exception ex)
  21:          {
  22:              MessageBox.Show(ex.Message, "exception");
  23:          }
  24:      }
  25:  }
当我们在Frame窗体中按下Enter键时,会将TextBoxEx窗体ShowDialog出来,等输入完一个网址,在按下Enter时,我们可以看到Frame窗体将加载你所输入的网页内容。
下面我们使用多行文本框来设计一个简单的记事本,使用固定路径存储。
   1:  using System;
   2:  using System.Collections.Generic;
   3:  using System.Linq;
   4:  using System.Text;
   5:  using System.Windows;
   6:  using System.Windows.Controls;
   7:  using System.Windows.Data;
   8:  using System.Windows.Documents;
   9:  using System.Windows.Input;
  10:  using System.Windows.Media;
  11:  using System.Windows.Media.Imaging;
  12:  using System.Windows.Shapes;
  13:   
  14:  namespace WPF_ButtonAndControls
  15:  {
  16:      /// <summary>
  17:      /// SmpileNotpad.xaml 的交互逻辑
  18:      /// </summary>
  19:      public partial class SmpileNotpad : Window
  20:      {
  21:          public SmpileNotpad()
  22:          {
  23:              InitializeComponent();
  24:              Loaded += WindowOnLoaded;
  25:          }
  26:   
  27:          TextBox text = null;
  28:          static string path = AppDomain.CurrentDomain.BaseDirectory + "file/test.txt";
  29:          private void WindowOnLoaded(object sender, RoutedEventArgs e)
  30:          {
  31:              text = new TextBox();
  32:              text.AcceptsReturn = true;
  33:              text.TextWrapping = TextWrapping.Wrap;
  34:              text.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
  35:   
  36:              Content = text;
  37:   
  38:              try
  39:              {
  40:                  text.Text = System.IO.File.ReadAllText(path);
  41:              }
  42:              catch (Exception ex)
  43:              {
  44:                  MessageBox.Show("没有找到此路径");
  45:              }
  46:   
  47:              text.CaretIndex = text.Text.Length;
  48:              text.Focus();
  49:          }
  50:   
  51:          protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
  52:          {
  53:              try
  54:              {
  55:                  System.IO.File.WriteAllText(path, text.Text);
  56:              }
  57:              catch (Exception ex)
  58:              {
  59:                  MessageBoxResult result = MessageBox.Show("File Not Saved,ReTry?", ex.Message, MessageBoxButton.YesNo, MessageBoxImage.Error);
  60:                  e.Cancel = result == MessageBoxResult.No;
  61:              }
  62:          }
  63:   
  64:          protected override void OnKeyDown(KeyEventArgs e)
  65:          {
  66:              if (e.Key == Key.F5)
  67:              {
  68:                  text.Text += DateTime.Now.ToString();
  69:                  text.CaretIndex = text.SelectionStart + text.SelectionLength;
  70:              }
  71:          }
  72:      }
  73:  }

这段代码演示了一个小的记事本,在关闭的时候会自动保存文章。

 

下面我们将讲解Rich Text Box控件,Rich Text Box控件要比TextBox控件复杂很多,因为它要保存字体的格式等等信息,你如果熟悉window中类似与Rich Text Box控件的话,你肯定认为Rich Text Box控件会按RTF(Rich Text Box Format)格式来进行保存,当然Rich Text Box控件确实支持RTF格式,它还支持另外的一种格式,既XML格式。

在下面的这个程序中,我们将使用Ctrl+O和Ctrl+S时,会有FIle open和File Save两个对话框进行调用,但是在这里,会有一个问题,Rich Text Box会抢先拦截键盘事件,所以我们在这个程序中使用preview事件来进行处理,在以后的学习中我们将进一步的学习处理方法。

   1:  using System;
   2:  using System.Collections.Generic;
   3:  using System.Linq;
   4:  using System.Text;
   5:  using System.Windows;
   6:  using System.Windows.Controls;
   7:  using System.Windows.Data;
   8:  using System.Windows.Documents;
   9:  using System.Windows.Input;
  10:  using System.Windows.Media;
  11:  using System.Windows.Media.Imaging;
  12:  using System.Windows.Shapes;
  13:  using Microsoft.Win32;
  14:  using System.IO;
  15:   
  16:  namespace WPF_ButtonAndControls
  17:  {
  18:      /// <summary>
  19:      /// RichTextBoxEx.xaml 的交互逻辑
  20:      /// 请使用 Ctrl+O,Ctrl+S打开,保存文件
  21:      /// Ctrl+I,Ctrl+B,Ctrl+U来给文字添加样式
  22:      /// </summary>
  23:      public partial class RichTextBoxEx : Window
  24:      {
  25:          public RichTextBoxEx()
  26:          {
  27:              InitializeComponent();
  28:              Loaded += WindowOnLoaded;
  29:          }
  30:   
  31:          RichTextBox rich = null;
  32:          string strFilter = "Document Files(*.xaml)|*.xaml|All Files(*.*)|*.*";
  33:          private void WindowOnLoaded(object sender, RoutedEventArgs e)
  34:          {
  35:              rich = new RichTextBox();
  36:              rich.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
  37:              Content = rich;
  38:              rich.Focus();
  39:          }
  40:          /// <summary>
  41:          /// 重载,抢在RichTextBox之前拦截事件
  42:          /// </summary>
  43:          /// <param name="e"></param>
  44:          protected override void OnPreviewTextInput(TextCompositionEventArgs e)
  45:          {
  46:              //ctrl+o
  47:              if (e.ControlText.Length > 0 && e.ControlText[0] == '\x0F')
  48:              {
  49:                  OpenFileDialog open = new OpenFileDialog();
  50:                  open.Filter = strFilter;
  51:                  open.CheckFileExists = true;
  52:   
  53:                  if ((bool)open.ShowDialog(this))
  54:                  {
  55:                      FlowDocument document = rich.Document;
  56:                      TextRange range = new TextRange(document.ContentStart, document.ContentEnd);
  57:   
  58:                      using (Stream stream = new FileStream(open.FileName,FileMode.Open))
  59:                      {
  60:                          try
  61:                          {
  62:                              range.Load(stream, DataFormats.Xaml);
  63:                          }
  64:                          catch (Exception ex)
  65:                          {
  66:                              MessageBox.Show(ex.Message);
  67:                          }
  68:                      }
  69:   
  70:                      e.Handled = true;
  71:                  }
  72:              }
  73:              //ctrl+s
  74:              if (e.ControlText.Length > 0 && e.ControlText[0] == '\x13')
  75:              {
  76:                  SaveFileDialog save = new SaveFileDialog();
  77:                  save.Filter = strFilter;
  78:   
  79:                  if ((bool)save.ShowDialog(this))
  80:                  {
  81:                      FlowDocument document = rich.Document;
  82:                      TextRange range = new TextRange(document.ContentStart, document.ContentEnd);
  83:   
  84:                      using (Stream stream = new FileStream(save.FileName,FileMode.Create))
  85:                      {
  86:                          try
  87:                          {
  88:                              range.Save(stream, DataFormats.Xaml);
  89:                          }
  90:                          catch (Exception ex)
  91:                          {
  92:                              MessageBox.Show(ex.Message);
  93:                          }
  94:                      }
  95:                      e.Handled = true;
  96:                  }
  97:              }
  98:              base.OnPreviewTextInput(e);
  99:          }
 100:      }
 101:  }
posted @ 2012-10-30 08:02  xx++  阅读(212)  评论(0编辑  收藏  举报