2017-12-01 8 views
-2

私は3つのアイテム(Room,Class,HighSchool)とテキストボックスを持つコンボボックスを持っています。ComboBoxでTextBoxを検索するには?

私はテキストボックス即時コンボボックスでRoomを書きます。

SelectedItem = Room 

答えて

0

カスタムコントロールを使用する方がよいでしょう。私はリストにバインドするIntellisenseテキストボックスを使用します。入力を開始すると、その文字でリスト内の項目が自動的に選択されます。

ウィンドウxaml;

<UserControl.Resources> 
    <Style x:Key="ListBoxItemStyle" TargetType="ListBoxItem"> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="ListBoxItem"> 
        <Border Name="_Border" 
          Padding="2" 
          SnapsToDevicePixels="true"> 
         <ContentPresenter /> 
        </Border> 
        <ControlTemplate.Triggers> 
         <Trigger Property="IsSelected" Value="true"> 
          <Setter TargetName="_Border" Property="Background" Value="Gray"/> 
         </Trigger> 
        </ControlTemplate.Triggers> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 
</UserControl.Resources> 
<Grid> 
    <TextBox Name="textbox" Grid.Column="1" Grid.Row="2" GotMouseCapture="textbox_GotMouseCapture" 
      GotFocus="textbox_GotFocus" PreviewKeyDown="textbox_PreviewKeyDown" TextChanged="textbox_TextChanged" 
      DataContext="{Binding ElementName=intelliWin}" 
      Text="{Binding Text, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width="{Binding ElementName=intelliWin, Path=Width}" TextAlignment="Center" Height="20"/> 
    <Popup Name="popup" Height="Auto" Width="Auto" MinWidth="180" StaysOpen="False" Placement="Bottom" 
       PlacementTarget="{Binding ElementName=textbox}" HorizontalAlignment="Left"> 
     <Popup.Style> 
      <Style TargetType="Popup"> 
       <Style.Triggers> 
        <Trigger Property="IsEnabled" Value="False"> 
         <Setter Property="IsOpen" Value="False"/> 
        </Trigger> 
       </Style.Triggers> 
      </Style> 
     </Popup.Style> 
     <Grid> 
      <ListBox Name="listbox" ScrollViewer.HorizontalScrollBarVisibility="Hidden" 
        MouseUp="listbox_MouseUp" ItemContainerStyle="{StaticResource ListBoxItemStyle}"> 
      </ListBox> 
     </Grid> 
    </Popup> 
</Grid> 

その後、

public partial class IntellisenseStyleTextbox : UserControl, INotifyPropertyChanged 
{ 
    public IntellisenseStyleTextbox() 
    { 
     InitializeComponent(); 
     (this.Content as FrameworkElement).DataContext = this; 
    } 

    public List<string> PossibleItems { get; set; } 
    public event EventHandler ItemSelected; 

    protected void OnItemSelected(EventArgs e) 
    { 
     EventHandler handler = ItemSelected; 
     if (handler != null) 
     { 
      handler(this, e); 
     } 
    } 

    // Bindable Text property from http://blog.jerrynixon.com/2013/07/solved-two-way-binding-inside-user.html 
    public string Text 
    { 
     get 
     { 
      return (string)GetValue(TextProperty); 
     } 
     set 
     { 
      SetValueDp(TextProperty, value); 
     } 
    } 

    // Required to allow items to bind to Text property 
    public static readonly DependencyProperty TextProperty = 
     DependencyProperty.Register("Text", typeof(string), 
      typeof(IntellisenseStyleTextbox), null); 

    public event PropertyChangedEventHandler PropertyChanged; 
    void SetValueDp(DependencyProperty property, object value, 
     [System.Runtime.CompilerServices.CallerMemberName] string p = null) 
    { 
     SetValue(property, value); 
     if (PropertyChanged != null) 
      PropertyChanged(this, new PropertyChangedEventArgs(p)); 
    } 

    public List<string> GetDisplayedModelNames() 
    { 
     // Only display items that have the current text at the start of them 
     List<string> toDisplay = new List<string>(); 
     string match = textbox.Text.ToLower(); 
     foreach (string possibleItem in PossibleItems) 
     { 
      string lower = possibleItem.ToLower(); 
      if (lower.Contains(match)) 
      { 
       toDisplay.Add(possibleItem); 
      } 
     } 

     return toDisplay; 
    } 

    private void textbox_PreviewKeyDown(object sender, System.Windows.Input.KeyEventArgs e) 
    { 
     switch (e.Key) 
     { 
      // Up and down arrow keys move up and down list box 
      case Key.Down: 
       listbox.SelectedIndex++; 
       listbox.ScrollIntoView(listbox.SelectedItem); 
       break; 
      case Key.Up: 
       // Make sure item is always selected 
       if (listbox.SelectedIndex > 0) 
       { 
        listbox.SelectedIndex--; 
        listbox.ScrollIntoView(listbox.SelectedItem); 
       } 

       break; 
      // Enter key fills in OutputName with selected text 
      case Key.Enter: 
       SelectListBoxItem(); 
       break; 
      case Key.Back: 
       popup.IsOpen = true; 
       break; 
     } 

     base.OnPreviewKeyDown(e); 
    } 

    private void SelectListBoxItem() 
    { 
     if (listbox.SelectedIndex < 0) return; 
     List<string> outputNames = GetDisplayedModelNames(); 

     if (listbox.SelectedIndex < outputNames.Count) 
     { 
      this.Text = outputNames[listbox.SelectedIndex]; 
      // Move caret to end of text 
      textbox.CaretIndex = this.Text.Count(); 
      popup.IsOpen = false; 
      OnItemSelected(new EventArgs()); 
     } 
    } 

    private void listbox_MouseUp(object sender, MouseButtonEventArgs e) 
    { 
     SelectListBoxItem(); 
    } 

    private void textbox_GotMouseCapture(object sender, MouseEventArgs e) 
    { 
     OpenPopup(); 
    } 

    private void textbox_GotFocus(object sender, RoutedEventArgs e) 
    { 
     OpenPopup(); 
    } 

    private void OpenPopup() 
    { 
     listbox.ItemsSource = GetDisplayedModelNames(); 
     popup.IsOpen = true; 
    } 

    private void textbox_TextChanged(object sender, TextChangedEventArgs e) 
    { 
     OnPropertyChanged(new DependencyPropertyChangedEventArgs(TextProperty, textbox.Text, textbox.Text)); 
     // Update listbox with relevant names 
     if (PossibleItems == null) return; 
     listbox.ItemsSource = this.GetDisplayedModelNames(); 
    } 
} 
ファイル.csファイルにこれを追加するには、この

<local:IntellisenseStyleTextbox Grid.Column="1" Grid.Row="6" Text="{Binding YOUR LIST DATA, UpdateSourceTrigger=PropertyChanged}" x:Name="intelliseText" Width="200" HorizontalContentAlignment="Center"/>' 

を行う必要があるウィンドウにそれを使用します

関連する問題