<i id='yibi3'><tr id='yibi3'><dt id='yibi3'><q id='yibi3'><span id='yibi3'><b id='yibi3'><form id='yibi3'><ins id='yibi3'></ins><ul id='yibi3'></ul><sub id='yibi3'></sub></form><legend id='yibi3'></legend><bdo id='yibi3'><pre id='yibi3'><center id='yibi3'></center></pre></bdo></b><th id='yibi3'></th></span></q></dt></tr></i><div id='yibi3'><tfoot id='yibi3'></tfoot><dl id='yibi3'><fieldset id='yibi3'></fieldset></dl></div>

    <small id='yibi3'></small><noframes id='yibi3'>

      <bdo id='yibi3'></bdo><ul id='yibi3'></ul>
  1. <tfoot id='yibi3'></tfoot>
  2. <legend id='yibi3'><style id='yibi3'><dir id='yibi3'><q id='yibi3'></q></dir></style></legend>

      WPF列表框通过单击空白区域删除选择

      WPF Listbox remove selection by clicking on blank space(WPF列表框通过单击空白区域删除选择)

          <bdo id='2ozXv'></bdo><ul id='2ozXv'></ul>

          <small id='2ozXv'></small><noframes id='2ozXv'>

              <i id='2ozXv'><tr id='2ozXv'><dt id='2ozXv'><q id='2ozXv'><span id='2ozXv'><b id='2ozXv'><form id='2ozXv'><ins id='2ozXv'></ins><ul id='2ozXv'></ul><sub id='2ozXv'></sub></form><legend id='2ozXv'></legend><bdo id='2ozXv'><pre id='2ozXv'><center id='2ozXv'></center></pre></bdo></b><th id='2ozXv'></th></span></q></dt></tr></i><div id='2ozXv'><tfoot id='2ozXv'></tfoot><dl id='2ozXv'><fieldset id='2ozXv'></fieldset></dl></div>
              <tfoot id='2ozXv'></tfoot>
                  <tbody id='2ozXv'></tbody>
              • <legend id='2ozXv'><style id='2ozXv'><dir id='2ozXv'><q id='2ozXv'></q></dir></style></legend>
                本文介绍了WPF列表框通过单击空白区域删除选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我有一个带有自定义项目模板的 wpf listbox,其中包含一个矩形.listbox 中的每一项都可以选择(一次只能选择一项).我想添加一种行为,当用户单击不是项目的地方(例如,listbox 上的空白点,它不是项目)时,所选项目将被取消选中.

                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 want to add a behavior in which when a user clicks on a place which isn't the item (for instance, a blank spot on the listbox, which is not an item), the selected item will become deselected.

                有什么想法吗?谢谢.

                以一个简单的列表框为例:第 1 项第 2 项

                For example with a simple listbox: item 1 item 2

                我正在寻找的行为是当用户点击像素 500(这是 listbox 的一部分但不是项目的一部分)时,当前选定的项目将被取消选择.

                The behavior that I'm looking for is when the user clicks on pixel 500 (which is a part of the listbox but not on an item), the currently selected item will be deselected.

                推荐答案

                简单的解决方案是将属性数据绑定到 ListBox.SelectedItem 属性并将其设置为 null 每当您想清除选择时:

                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;
                

                你什么时候会这样做?您可以将处理程序附加到 Window 或 UI 中的任何其他控件的 ">PreviewMouseLeftButtonDown 事件.在处理程序方法中,您可以进行命中测试以查看用户单击的项目是什么:

                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>();
                

                请参阅 VisualTreeHelper.HitTestMethod (Visual, Point) 获取更多关于这部分的帮助.

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

                然后可能是这样的:

                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.

                编辑>>>

                通用 GetParentOfType 方法是自定义 扩展方法,在一个名为 DependencyObjectExtensions 的单独类中定义:

                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);
                    }
                
                    ...
                }
                

                这篇关于WPF列表框通过单击空白区域删除选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Populate ListBox with a IEnumrable on another thread (winforms)(在另一个线程(winforms)上使用 IEnumrable 填充 ListBox)
                listbox selected item give me quot; System.Data.DataRowViewquot; , C# winforms(列表框选择的项目给我quot;System.Data.DataRowView, C# Winforms)
                Cannot remove items from ListBox(无法从列表框中删除项目)
                Preventing ListBox scrolling to top when updated(更新时防止列表框滚动到顶部)
                Drag and drop from list to canvas on windows phone with MVVM(使用 MVVM 在 Windows 手机上从列表拖放到画布)
                Deselection on a WPF listbox with extended selection mode(具有扩展选择模式的 WPF 列表框上的取消选择)

                <tfoot id='tovzP'></tfoot>
                  <bdo id='tovzP'></bdo><ul id='tovzP'></ul>
                      <tbody id='tovzP'></tbody>

                        <i id='tovzP'><tr id='tovzP'><dt id='tovzP'><q id='tovzP'><span id='tovzP'><b id='tovzP'><form id='tovzP'><ins id='tovzP'></ins><ul id='tovzP'></ul><sub id='tovzP'></sub></form><legend id='tovzP'></legend><bdo id='tovzP'><pre id='tovzP'><center id='tovzP'></center></pre></bdo></b><th id='tovzP'></th></span></q></dt></tr></i><div id='tovzP'><tfoot id='tovzP'></tfoot><dl id='tovzP'><fieldset id='tovzP'></fieldset></dl></div>

                      1. <small id='tovzP'></small><noframes id='tovzP'>

                        1. <legend id='tovzP'><style id='tovzP'><dir id='tovzP'><q id='tovzP'></q></dir></style></legend>