WPF Listbox remove selection by clicking on blank space

后端 未结 4 657
旧时难觅i
旧时难觅i 2021-01-03 03:49

I have a wpf listbox with a custom item template which contains a rectangle. The each item in the listbox can be selected (only one at a time). I w

相关标签:
4条回答
  • 2021-01-03 04:02

    To assure that only one item is selected put this in the listbox:

    SelectionMode="Single"
    

    then for the unselect when clicking somewhere, you can try to check this events

    PreviewMouseLeftButtonUp
    LostFocus()
    

    Regards,

    0 讨论(0)
  • 2021-01-03 04:09

    For The each item in the listbox can be selected (only one at a time).

    You can come up with one of followings

    1- Disable the item after it is selected.

    2- Maintain a list at backend to mark each index selectable or unselectable.

    0 讨论(0)
  • 2021-01-03 04:14

    The simple solution is to data bind a property to the ListBox.SelectedItem property and set it to null whenever you want to clear the selection:

    <ListBox ItemsSource="{Binding YourItems}" SelectedItem="{Binding SelectedItem}" 
        SelectionMode="Single" />
    

    Then in code, you can just do this to clear the selection:

    SelectedItem = null;
    

    And when would you do that? You can attach a handler to the PreviewMouseLeftButtonDown event of the Window, or any other control in your UI. In the handler method, you could do a hit test to see what the item the user clicked on was:

    HitTestResult hitTestResult = 
        VisualTreeHelper.HitTest(controlClickedOn, e.GetPosition(controlClickedOn));
    Control controlUnderMouse = hitTestResult.VisualHit.GetParentOfType<Control>();
    

    See the VisualTreeHelper.HitTest Method (Visual, Point) for more help with this part.

    Then maybe something like this:

    if (controlUnderMouse.GetType() != typeof(ListBoxItem)) SelectedItem = null;
    

    Of course, there are many ways to do this, and you'll have to fill in the few blank spots that I left, but you should get the idea.


    EDIT >>>

    The generic GetParentOfType method is a custom Extension Method that is defined in a separate class named DependencyObjectExtensions:

    public static class DependencyObjectExtensions
    {
        public static T GetParentOfType<T>(this DependencyObject element) 
            where T : DependencyObject
        {
            Type type = typeof(T);
            if (element == null) return null;
            DependencyObject parent = VisualTreeHelper.GetParent(element);
            if (parent == null && ((FrameworkElement)element).Parent is DependencyObject) 
                parent = ((FrameworkElement)element).Parent;
            if (parent == null) return null;
            else if (parent.GetType() == type || parent.GetType().IsSubclassOf(type)) 
                return parent as T;
            return GetParentOfType<T>(parent);
        }
    
        ...
    }
    
    0 讨论(0)
  • 2021-01-03 04:23

    Try ensuring your ListBox's background color is 'Transparent'

    0 讨论(0)
提交回复
热议问题