WPF Listbox Show Button in ItemTemplate on MouseOver

后端 未结 5 1508
既然无缘
既然无缘 2020-12-23 09:56

I have a listbox containing and image and a button. By default the button is hidden. I want to make the button visible whenever I hover over an item in the listbox. The XAML

5条回答
  •  心在旅途
    2020-12-23 10:09

    Just wondering, if we use the technique above, how do we determine what item the button was clicked on?

    To answer Brian's question, in the button click handler you can walk up the visual tree to find the item that contains the button:

            DependencyObject dep = (DependencyObject)e.OriginalSource;
            while ((dep != null) && !(dep is ListBoxItem))
            {
                dep = VisualTreeHelper.GetParent(dep);
            }
    
            if (dep != null)
            {
                // TODO: do stuff with the item here.
            }
    

提交回复
热议问题