轉(zhuǎn)帖|其它|編輯:郝浩|2011-04-18 13:36:34.000|閱讀 1146 次
概述:前幾日想給WPF的RichTextBox新增上智能感知的功能,搜了一圈沒(méi)有找到合適的開(kāi)源代碼,于是自己花了點(diǎn)時(shí)間搞定了它,小小的擴(kuò)展了一下RichTextBox。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
前幾日想給WPF的RichTextBox新增上智能感知的功能,搜了一圈沒(méi)有找到合適的開(kāi)源代碼,于是自己花了點(diǎn)時(shí)間搞定了它,小小的擴(kuò)展了一下RichTextBox,先看效果圖:
怎么使用這個(gè)擴(kuò)展后的RichTextBox
擴(kuò)展后的RTB新增了幾個(gè)依賴屬性:
ContentAssistSource:智能感知數(shù)據(jù)源
ContentAssistTriggers:智能感知觸發(fā)器(即當(dāng)輸入哪些字符時(shí)會(huì)顯示智能感知)
AutoAddWhiteSpaceAfterTriggered:當(dāng)選擇提示中的某一項(xiàng)時(shí),是否自動(dòng)增加空格
可以直接在xaml中這樣使用:
<rabbit:RichTextBoxEx Name="richTextBoxEx1"
AutoAddWhiteSpaceAfterTriggered="{Binding IsChecked,ElementName=chkAutoAddWhitespace}"
ContentAssistTriggers="{Binding ContentAssistTriggers}"
ContentAssistSource="{Binding ContentAssistSource}" />
很簡(jiǎn)單吧?
如何實(shí)現(xiàn)的?
一、準(zhǔn)備
為了實(shí)現(xiàn)這一功能,我首先在擴(kuò)展后的rtb中添加一個(gè)ListBox,作為智能提示數(shù)據(jù)源的載體。在rtb 加載后將ListBox添加到它的父容器中(為了方便控制位置,此處強(qiáng)制父容器為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 事件,就是為了方便用戶選擇提示的內(nèi)容。事件中都做了哪些事件呢?就是簡(jiǎn)單的將當(dāng)前選中的Item的值插入到RichTextbox中。在此不貼代碼了。
二、如何顯示
做完準(zhǔn)備工作后,如何在用戶輸入某些特定的字母后,出現(xiàn)對(duì)應(yīng)的提示呢?
其實(shí)也挺簡(jiǎn)單,重寫(xiě)一下OnTextInput事件,在該事件里做一些判斷,如果滿足出現(xiàn)提示的條件,就把ListBox給顯示到合適的位置,同時(shí)再做一些其他的工作,就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();
}
}
接下來(lái)再override一個(gè)OnPreviewKeyDown事件,處理一下用戶的按鍵事件,比如當(dāng)用戶按下enter或tab時(shí),就表明用戶想選擇當(dāng)前的第一項(xiàng),當(dāng)按下Down鍵時(shí),意味著用戶想選擇下一項(xiàng)等等,如下代碼所示:
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);
}
到現(xiàn)在為止,擴(kuò)展的richtextbox已經(jīng)具備了智能感知的功能。上面的幾個(gè)代碼塊中都用到了一個(gè)方法,F(xiàn)ilterAssistBoxItemsSource(),它是負(fù)責(zé)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;
}
}
有感興趣的,就下載下來(lái)演示看看。感覺(jué)到對(duì)自己的項(xiàng)目有用的,就下下來(lái)源碼看看。如果覺(jué)得不符合自己的需求,就改改。
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn