3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【WPF】ComboBoxの項目をマウスホバー+Deleteで削除する記事

Last updated at Posted at 2025-09-14

[WPF] Remove ComboBox Items(dropDownList) with Mouse Hover + Delete

とりあえず必要だったので作成。
イベントから取得するだけなのでCode的には案外簡単。

3いいね以下で消し(1年後まで)。

必要なもの

  • イベント関連
    comboBox_MouseMove イベント
    comboBox_PreviewKeyDownイベント
  • フィールド
    private ObservableCollection<KeyValuePair<string, string>> dic
  • メソッド
    FindParentメソッド(VisualTreeHelper.GetParent)

実行結果(GIF)

※これも条件つけて....なんてやったら餌にもならない

bandicam 2025-09-15 03-22-45-785_Harua.mp4.gif

Code

using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;

namespace ComboBoxItemsDeleate
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }



        private ObservableCollection<KeyValuePair<string, string>> dic;

        //  private Dictionary<string, string> dic; // Dictionaryをフィールドに移動


        string key = string.Empty;
        string value = string.Empty;
        KeyValuePair<string, string> dicPaire;


        private void comboBox_MouseMove(object sender, MouseEventArgs e)
        {
            var comboBox = sender as ComboBox;
            if (comboBox == null || !comboBox.IsDropDownOpen) return;

            // マウス下の要素を取得
+           var element = e.OriginalSource as DependencyObject;
            if (element == null) return;

            // ListBoxItem を辿って探す
+            var listBoxItem = FindParent<ListBoxItem>(element);
            if (listBoxItem == null) return;


            // 中身を取り出す
            if (listBoxItem.Content is KeyValuePair<string, string> item)
            {
                key = item.Key;
                value = item.Value;
                dicPaire = item;

                System.Diagnostics.Debug.WriteLine($"Hovered item: {value}, Key: {key}");
            }

        }


        private T? FindParent<T>(DependencyObject child) where T : DependencyObject
        {
            DependencyObject parent = child;
            while (parent != null && parent is not T)
            {
                parent = VisualTreeHelper.GetParent(parent);
            }
            return parent as T;
        }

        private void comboBox_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (string.IsNullOrEmpty(key))
                return;
            if (e.Key == Key.Delete)
            {
                if (dic.Contains(dicPaire))
                {
                    dic.Remove(dicPaire);
                    // ItemsSourceを再設定してUIを更新

                    key = string.Empty; // 選択をクリア
                }
            }


        }

        private void comboBox_Loaded(object sender, RoutedEventArgs e)
        {
            dic = new()
            {
             new KeyValuePair<string, string>("メアリー", "ひつじ"),
        new KeyValuePair<string, string>("メアリベリー", "妖怪"),
        new KeyValuePair<string, string>("真木よう子", "昭和的女優")
            };

            comboBox.ItemsSource = dic;
        }
    }
}

以上です。

関連記事

意外と...かなり少ない。

ComboBox firing events when hovering on the dropdownlist

AI的纒

  • カスタムコントロール HoverComboBox の実装

  • ポイント: GetHoveredComboBoxItemメソッドは、マウスの位置に基づいてホバー中のComboBoxItemを特定。これにより、どのアイテムがホバーされているかを正確に把握可能。

  • カスタムイベント ComboBoxItemMouseEnter

なぜ関連記事が必要なのか?

更に実装の手がかりとバリエーションを得るのに役に立つためです。
仕様の理解にもさまざまなドキュメントが必要です。

如何でしたか?

この記事が面白くなかったらブラウザバック、フィルタリング。記事タイトルにセンスがないと感じたら通報して頂けると励みになります。

$\color{lightgray}{ \textsf{ その手のひねくれ者もいるらしい}}$

3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?