效果:
<DockPanel Grid.Row="1" LastChildFill="True">
<widgets:SearchBox x:Name="SearchBox_Input"
ShowSearchIcon="False"
HintText="Please input command."
FontSize="16"
FontFamily="Consolas"
Height="32"
OnTextCommitted="SearchBox_OnTextCommitted"
OnSearching="SearchBox_OnSearching"
PopupPlacement="Top"
WidthDisplay="Auto"
PopupBackground="{DynamicResource StyleBackgroundBrush}"
ForceCallTextCommittedEvent="True"/>
</DockPanel>
ShowSearchIcon 設置爲false的話不會顯示最右邊搜索的圖表
OnTextCommitted事件 當鍵盤按下Enter鍵後調用的
private void SearchBox_OnTextCommitted(object sender, Wpf.Widgets.SearchBoxTextCommittedEventArgs e)
{
//e.Text 是提交的文本
if (string.IsNullOrEmpty(e.Text))
{
return;
}
Base.CmdExecutor.ExcuteCommand(this, e.Text);
//設置爲true,表示之後將會清空搜索框,默認false
e.ClearText = true;
}
OnSearching事件 輸入文本時調用
private void SearchBox_OnSearching(object sender, Wpf.Widgets.SearchBoxSearchingEventArgs e)
{
//e.Keyword 就是當前文本框中的文本
Launcher.Current.Command.FindCommands(e.Keyword, out List<Base.ConsoleMethodInfo> Cmds);
foreach(var cmd in Cmds)
{
//e.Result 是返回的結果集合
e.Result.Add(cmd.Method.Name);
}
}
按鍵盤上下鍵可以切換輸入的記錄,初始化記錄列表
if (Properties.Settings.Default.InputHistories == null)
{
Properties.Settings.Default.InputHistories = new System.Collections.Specialized.StringCollection();
}
//第一個參數表示是否重置輸入記錄的列表
SearchBox_Input.AddInputHistories(true, Properties.Settings.Default.InputHistories.GetEnumerator());
保存
Properties.Settings.Default.InputHistories.Clear();
//獲取輸入的記錄
var inputHistories = SearchBox_Input.GetInputHistories();
foreach(var input in inputHistories)
{
Properties.Settings.Default.InputHistories.Add(input);
}
Properties.Settings.Default.Save();
輸入記錄的上限設置
//記錄的上限,達到上限會刪除第一個記錄然後往後添加
SearchBox_Input.InputHistoriesLimitCount;
搜索結果的寬度顯示設置
//寬度顯示設置
SearchBox_Input.WidthDisplay;
/// <summary>
/// 搜索結果寬度顯示類型
/// </summary>
public enum SearchResultWindowWidthDisplay
{
/// <summary>
/// 寬度取列表子項的最大寬度
/// </summary>
Auto,
/// <summary>
/// 和搜索框一樣大小
/// </summary>
SameAsOwner,
/// <summary>
/// 固定寬度
/// </summary>
Fixed
}
當設置爲Fixed時,需要設置這個變量
//搜索結果的窗口寬度(只有WidthDisplay設置爲Fixed纔會生效)
SearchBox_Input.SearchResultWindowWidth;
等等還有一些設置可以看SearchBox.cs裏的源碼的註釋