classicjonesynz
classicjonesynz

Reputation: 4042

Removing selected items (highlighted) from DropDown

I'm wondering is it possible to remove highlighted items with Key.Delete, while a DropDown is Open?

example
(source: iforce.co.nz)

What I've tried..

C#

    private void OnKeyUpHandler(object sender, KeyEventArgs e)
    {
        Boolean delete = e.Key == Key.Delete;
        if (sender == cbkSnpCodes && (delete && cbkSnpCodes.IsDropDownOpen))
        {
            cbkSnpCodes.Items.Remove(cbkSnpCodes.SelectedItem);
            cbkSnpCodes.Items.Refresh();
        }
    }

XAML

<ComboBox ItemsSource="{Binding SnpCodeModel}" Name="cbkSnpCodes" Controls:TextboxHelper.Watermark="Enter an snp code and press enter" MaxDropDownHeight="50" KeyUp="OnKeyUpHandler" KeyDown="OnKeyDownHandler" DisplayMemberPath="SnpCode" IsEditable="True" HorizontalAlignment="Left" VerticalAlignment="Top" Text="{Binding Path=SnpCode, Mode=TwoWay}" IsReadOnly="False" Height="26" Margin="81,9,0,0" Width="223" Grid.Column="1" Grid.ColumnSpan="2" />

Binding Class SnpCodeModel

class SnpCodeModel
{
    public string SnpCode { get; set; }

    public SnpCodeModel(string _snpcode)
    {
            this.SnpCode = _snpcode;
    }
}

I have also tried the solution posted by Harold Bamford on the Stackoverflow question: In a combobox, how do I determine the highlighted item (not selected item)?

    private void OnKeyUpHandler(object sender, KeyEventArgs e)
    {
        Boolean delete = e.Key == Key.Delete;
        ComboBox box = sender as ComboBox;
        if (box.IsDropDownOpen && delete)
        {
            const BindingFlags flags = BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance;
            PropertyInfo hl = box.GetType().GetProperty("HighlightedItem", flags);
            if (hl != null)
            {
                String hlString = hl.GetValue(sender, null).ToString();
                SnpCodeModel snp = new SnpCodeModel(hlString);
                cbkSnpCodes.Items.Remove(snp);
                cbkSnpCodes.Items.Refresh();
            }
        }
    }

But it doesn't work :-( and produces this output within the System.Diagnostics;

System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.ItemsControl', AncestorLevel='1''. BindingExpression:Path=VerticalContentAlignment; DataItem=null; target element is 'ComboBoxItem' (Name=''); target property is 'VerticalContentAlignment' (type 'VerticalAlignment')

The main problem with my current attempts is that because the ComboBox is editable the item that I am trying to Delete becomes null before it reaches cbkSnpCodes.Items.Remove.

Is it possible to achieve the functionality I'm looking for? I haven't been able to find much on google

Upvotes: 1

Views: 602

Answers (1)

Florian Gl
Florian Gl

Reputation: 6014

You are creating a new instance of SnpCodeModel with SnpCodeModel snp = new SnpCodeModel(hlString);. Even though it has the same content, it cant be found in cbkSnpCodes, because its not the same instance as the HighlightedItem.

Try something like

        if (hl != null)
        {
            box.Items.Remove(hl.GetValue(sender, null));
            //box.Items.Refresh();
        }

or override the Equals method in your SnpCodeModel:

    public override bool Equals(object obj)
    {
        if (obj is SnpCodeModel)
            return ((SnpCodeModel)obj).SnpCode == this.SnpCode

        return false;
    }

Upvotes: 1

Related Questions