AutoCompleteBox
WPF Toolkitを使用して検索フィールドを構築しようとしています。AutoCompleteBox の Text プロパティは、ViewModel
を実装する のプロパティにバインドされていますINotifyPropertyChanged
。プロパティが変更されると、ユーザーに表示する新しい提案がフェッチされます。
ユーザーが矢印キーを使用してオートコンプリート候補のリストをスキャンしてから 1 つを選択すると、これは混乱します。カーソルがポップアップに移動した瞬間にSelectionChanged
、テキスト フィールドが新しい値を取得し、オートコンプリート候補が再収集されます。 . SelectionChanged
これはまた、イベントを使用して検索を開始したいという私の欲求を妨げます。
キーボード ナビゲーションで SelectionChanged イベントが発生しないようにする方法はありますか?
これが私が物事をセットアップする方法です。sc:SearchField
のサブクラスであり、のプロパティにAutoCompleteBox
アクセスする方法のみを提供するため、次のような関数を呼び出すことができますTextBox
AutoCompleteBox
SelectAll()
XAML:
<sc:SearchField x:Name="SearchField" DataContext="{Binding SearchBoxVm}" Text="{Binding Query, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" ItemsSource="{Binding QuerySuggestions, UpdateSourceTrigger=PropertyChanged}" SelectedItem="{Binding SelectedItem, Mode=TwoWay}" IsTextCompletionEnabled="False" Margin="54,10,117,67" Grid.RowSpan="2" BorderThickness="0" FontSize="14" PreviewKeyUp="searchField_OnKeyup" Foreground="{Binding Foreground, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" FontStyle="{Binding QueryFont, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" >
</sc:SearchField>
ビューモデル:
void GetQuerySuggestions()
{
if (!string.IsNullOrEmpty(Query) && !Query.Equals(DEFAULT_TEXT))
{
QueryFont = FontStyles.Normal;
Foreground = Brushes.Black;
QuerySuggestions = SearchAssistant.GetQueryRecommendations(_query);
}
}
public string _query = DEFAULT_TEXT;
public string Query
{
get
{
return _query;
}
set
{
_query = value;
GetQuerySuggestions();
NotifyPropertyChanged("Query");
}
}
List<string> querySuggestions = new List<string>();
public List<string> QuerySuggestions
{
get { return querySuggestions; }
set
{
querySuggestions = value;
NotifyPropertyChanged("QuerySuggestions");
}
}
SearchField サブクラス:
public class SearchField : AutoCompleteBox
{
public TextBox TextBox
{
get
{
return (this.GetTemplateChild("Text") as TextBox);
}
}
}