轉帖|其它|編輯:郝浩|2011-04-18 13:36:34.000|閱讀 1146 次
概述:前幾日想給WPF的RichTextBox新增上智能感知的功能,搜了一圈沒有找到合適的開源代碼,于是自己花了點時間搞定了它,小小的擴展了一下RichTextBox。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
前幾日想給WPF的RichTextBox新增上智能感知的功能,搜了一圈沒有找到合適的開源代碼,于是自己花了點時間搞定了它,小小的擴展了一下RichTextBox,先看效果圖:
怎么使用這個擴展后的RichTextBox
擴展后的RTB新增了幾個依賴屬性:
ContentAssistSource:智能感知數據源
ContentAssistTriggers:智能感知觸發器(即當輸入哪些字符時會顯示智能感知)
AutoAddWhiteSpaceAfterTriggered:當選擇提示中的某一項時,是否自動增加空格
可以直接在xaml中這樣使用:
<rabbit:RichTextBoxEx Name="richTextBoxEx1"
AutoAddWhiteSpaceAfterTriggered="{Binding IsChecked,ElementName=chkAutoAddWhitespace}"
ContentAssistTriggers="{Binding ContentAssistTriggers}"
ContentAssistSource="{Binding ContentAssistSource}" />
很簡單吧?
如何實現的?
一、準備
為了實現這一功能,我首先在擴展后的rtb中添加一個ListBox,作為智能提示數據源的載體。在rtb 加載后將ListBox添加到它的父容器中(為了方便控制位置,此處強制父容器為Grid),如下代碼片斷:
private ListBox AssistListBox = new ListBox();
void RichTextBoxEx_Loaded(object sender, RoutedEventArgs e)
{
//init the assist list box
if (this.Parent.GetType() != typeof(Grid))
{
throw new Exception( "this control must be put in Grid control");
}
if (ContentAssistTriggers.Count == 0)
{
ContentAssistTriggers.Add('@');
}
(this.Parent as Grid).Children.Add(AssistListBox);
AssistListBox.MaxHeight = 100;
AssistListBox.MinWidth = 100;
AssistListBox.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
AssistListBox.VerticalAlignment = System.Windows.VerticalAlignment.Top;
AssistListBox.Visibility = System.Windows.Visibility.Collapsed;
AssistListBox.MouseDoubleClick += new MouseButtonEventHandler(AssistListBox_MouseDoubleClick);
AssistListBox.PreviewKeyDown += new KeyEventHandler(AssistListBox_PreviewKeyDown);
}
你看到了,我給ListBox新增了MouseDoubleClick 和PreviewKeyDown 事件,就是為了方便用戶選擇提示的內容。事件中都做了哪些事件呢?就是簡單的將當前選中的Item的值插入到RichTextbox中。在此不貼代碼了。
二、如何顯示
做完準備工作后,如何在用戶輸入某些特定的字母后,出現對應的提示呢?
其實也挺簡單,重寫一下OnTextInput事件,在該事件里做一些判斷,如果滿足出現提示的條件,就把ListBox給顯示到合適的位置,同時再做一些其他的工作,就ok了:
protected override void OnTextInput(System.Windows.Input.TextCompositionEventArgs e)
{
base.OnTextInput(e);
if (IsAssistKeyPressed == false && e.Text.Length == 1)
{
if (ContentAssistTriggers.Contains(char.Parse(e.Text)))
{
ResetAssistListBoxLocation();
IsAssistKeyPressed = true;
FilterAssistBoxItemsSource();
return;
}
}
if (IsAssistKeyPressed)
{
sbLastWords.Append(e.Text);
FilterAssistBoxItemsSource();
}
}
接下來再override一個OnPreviewKeyDown事件,處理一下用戶的按鍵事件,比如當用戶按下enter或tab時,就表明用戶想選擇當前的第一項,當按下Down鍵時,意味著用戶想選擇下一項等等,如下代碼所示:
protected override void OnPreviewKeyDown(System.Windows.Input.KeyEventArgs e)
{
if (!IsAssistKeyPressed)
{
base.OnPreviewKeyDown(e);
return;
}
ResetAssistListBoxLocation();
if (e.Key == System.Windows.Input.Key.Back)
{
if (sbLastWords.Length > 0)
{
sbLastWords.Remove(sbLastWords.Length - 1, 1);
FilterAssistBoxItemsSource();
}
else
{
IsAssistKeyPressed = false;
sbLastWords.Clear();
AssistListBox.Visibility = System.Windows.Visibility.Collapsed;
}
}
//enter key pressed, insert the first item to richtextbox
if ((e.Key == Key.Enter || e.Key == Key.Space || e.Key == Key.Tab))
{
AssistListBox.SelectedIndex = 0;
if (InsertAssistWord())
{
e.Handled = true;
}
}
if (e.Key == Key.Down)
{
AssistListBox.Focus();
}
base.OnPreviewKeyDown(e);
}
到現在為止,擴展的richtextbox已經具備了智能感知的功能。上面的幾個代碼塊中都用到了一個方法,FilterAssistBoxItemsSource(),它是負責ListBox的顯示或是隱藏的:
private void FilterAssistBoxItemsSource()
{
IEnumerable <string> temp = ContentAssistSource.Where(s => s.ToUpper().StartsWith(sbLastWords.ToString().ToUpper()));
AssistListBox.ItemsSource = temp;
AssistListBox.SelectedIndex = 0;
if (temp.Count() == 0)
{
AssistListBox.Visibility = System.Windows.Visibility.Collapsed;
}
else
{
AssistListBox.Visibility = System.Windows.Visibility.Visible;
}
}
有感興趣的,就下載下來演示看看。感覺到對自己的項目有用的,就下下來源碼看看。如果覺得不符合自己的需求,就改改。
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn