如何使用TextBox在ComboBox中搜索?

时间:2017-12-01 11:08:10

标签: combobox

我有一个包含3个项目(Room,Class,HighSchool)和一个TextBox的组合框。

我希望我立即在文本框中写Room组合框。

SelectedItem = Room

1 个答案:

答案 0 :(得分:0)

最好使用自定义控件。我使用绑定到列表的智能感知文本框,当您开始输入时,它将自动选择列表中包含该字符的任何项目。

下面的窗口xaml;

public static IEnumerable<Byte> ToBytes(this BitArray bits, bool MSB = false)
    {
        int bitCount = 7;
        int outByte = 0;

        foreach (bool bitValue in bits)
        {
            if (bitValue)
                outByte |= MSB ? 1 << bitCount : 1 << (7 - bitCount);
            if (bitCount == 0)
            {
                yield return (byte) outByte;
                bitCount = 8;
                outByte = 0;
            }
            bitCount--;
        }
        // Last partially decoded byte
        if (bitCount < 7)
            yield return (byte) outByte;
    }
}

在您需要执行此操作的窗口中使用它

<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>

然后将其添加到.cs文件

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