2013-08-15 2 views
1

WPF Toolkit에서 AutoCompleteBox을 사용하여 검색 필드를 작성하려고합니다. AutoCompleteBox의 Text 속성은 ViewModel에있는 INotifyPropertyChanged을 구현하는 속성에 바인딩됩니다. 속성이 변경되면 사용자에게 표시 할 새 제안을 가져옵니다.AutoCompleteBox에서 키보드 탐색으로 SelectionChanged 이벤트가 발생하지 않도록하는 방법은 무엇입니까?

사용자가 커서 키를 사용하기 전에 화살표 키를 사용하여 자동 완성 제안 목록을 검색하면 커서가 팝업으로 이동하고 SelectionChanged이 실행되고 텍스트 필드가 새로운 값을 얻으며 자동 완성 제안은 다시 수집됩니다. 또한 검색을 시작하기 위해 SelectionChanged 이벤트를 사용하려는 나의 욕구를 저해합니다.

키보드 탐색에서 SelectionChanged 이벤트가 실행되지 않도록 할 수있는 방법이 있습니까?

다음은 설정 방법입니다.

<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 sc:SearchField 단지 내가 SelectAll()

XAML과 같은 함수를 호출 할 수있는 AutoCompleteBoxTextBox 속성에 액세스 할 수있는 방법을 제공합니다 AutoCompleteBox의 서브 클래스 참고 하위 클래스 :

public class SearchField : AutoCompleteBox 
{ 
    public TextBox TextBox 
    { 
     get 
     { 
      return (this.GetTemplateChild("Text") as TextBox); 
     } 
    }   
} 

답변

1

이것이 원하는 것인지 확실하지 않지만 'Enter'키를 누르거나 마우스를 사용하여 목록에서 항목을 선택하는 경우 (왼쪽 마우스 단추를 클릭 한 경우) 선택을 변경하는 다음 코드가 있습니다. . 나는 문제없이 목록의 위아래로 화살표를 바꿀 수 있으며 사용자가 원하는 항목을 입력하거나 클릭 할 때 선택 변경 이벤트 만 발생시킵니다.

사용중인 SearchField가 아닌 AutoCompleteBox를 사용하고 있습니다. XAML에서

: 뷰 모델에서

private void OmniSearch_KeyUp(object sender, KeyEventArgs e) 
{ 
    if (e.Key == Key.Enter) 
    { 
     BindingExpression exp = this.OmniSearchTextBox.GetBindingExpression(AutoCompleteBox.SelectedItemProperty); 
     exp.UpdateSource(); 
    } 
} 

private void OmniSearch_MouseLeftButtonUp(object sender, MouseEventArgs e) 
{ 
    BindingExpression exp = this.OmniSearchTextBox.GetBindingExpression(AutoCompleteBox.SelectedItemProperty); 
    exp.UpdateSource(); 
} 

:

private const string CompanyListPropertyName = "CompanyList"; 
private ObservableCollection<Company> _companyList; 
public ObservableCollection<Company> CompanyList 
{ 
    get 
    { 
     return _companyList; 
    } 
    set 
    { 
     if (_companyList == value) 
     { 
      return; 
     } 

     _companyList = value; 
     RaisePropertyChanged(CompanyListPropertyName); 
    } 

} 

private Company _selectedObject; 
public Company SelectedObject 
{ 
    get 
    { 
     return _selectedObject; 
    } 
    set 
    { 
     if (_selectedObject != value) 
     { 
      _selectedObject = value; 
     } 
    } 
} 
뒤에 코드에서

<toolkit:AutoCompleteBox Name="OmniSearchTextBox" 
         ItemsSource="{Binding CompanyList}" 
         SelectedItem="{Binding SelectedObject, Mode=TwoWay}" 
         IsTextCompletionEnabled="False" 
         FilterMode="Contains" 
         KeyUp="OmniSearch_KeyUp" 
         MouseLeftButtonUp="OmniSearch_MouseLeftButtonUp" 
         Margin="10,94,10,0" 
         RenderTransformOrigin="0.518,1.92" Height="35" 
         VerticalAlignment="Top" />