简体   繁体   中英

WPF ComboBox disallow selection

I have a ComboBox that is being declared as follows:

<ComboBox 
    Name="cmb" Height="20" Width="125" Margin="5,3,0,0" 
    Text="{Binding SomeList}"
    UpdateSourceTrigger=PropertyChanged}" 
    Validation.ErrorTemplate="{StaticResource errorTemplate}"
    IsEditable ="True"
    IsSynchronizedWithCurrentItem="True"/>

I'm trying to filter the list of items based on what's typed in the ComboBox . Now, I've tried custom controls, handling the KeyUp event of the ComboBox and so on but the one problem I constantly come across is that the moment I type a letter in the ComboBox the very first matching item is populated in the TextBox and then the filter returns only that matching item making the list have only one item. Here is the KeyUp event handler and the FilterPredicate :

private bool FilterPredicate(object value)
{
    if (value == null)
        return false;

    if (cmb.Text.Length == 0)
        return true;

    string prefix = cmb.Text;

    return value.ToString().ToLower().Contains(prefix.ToLower());
}

private void cmb_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
{
    ComboBox cbSender = sender as ComboBox;
    cbSender.Items.Filter = null;
    if (e.Key == System.Windows.Input.Key.Escape || e.Key == System.Windows.Input.Key.Tab ||
        e.Key == System.Windows.Input.Key.Enter)
    {
        cbSender.IsDropDownOpen = false;
    }
    else if (e.Key == System.Windows.Input.Key.Down)
    {
        cbSender.IsDropDownOpen = true;
    }
    else
    {
        if (cbSender.HasItems)
        {
            cbSender.IsDropDownOpen = true;
        }
    }
}

The Filter for the ComboBox is bound to the FilterPredicate in the constructor.

Let me know if you need more information.

Thank you

禁用isTextSearchEnabled属性对我有用。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM