• <legend id='OntZZ'><style id='OntZZ'><dir id='OntZZ'><q id='OntZZ'></q></dir></style></legend>

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

        • <bdo id='OntZZ'></bdo><ul id='OntZZ'></ul>

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

        具有扩展选择模式的 WPF 列表框上的取消选择

        时间:2023-10-08

              <i id='YqbPu'><tr id='YqbPu'><dt id='YqbPu'><q id='YqbPu'><span id='YqbPu'><b id='YqbPu'><form id='YqbPu'><ins id='YqbPu'></ins><ul id='YqbPu'></ul><sub id='YqbPu'></sub></form><legend id='YqbPu'></legend><bdo id='YqbPu'><pre id='YqbPu'><center id='YqbPu'></center></pre></bdo></b><th id='YqbPu'></th></span></q></dt></tr></i><div id='YqbPu'><tfoot id='YqbPu'></tfoot><dl id='YqbPu'><fieldset id='YqbPu'></fieldset></dl></div>
                  <bdo id='YqbPu'></bdo><ul id='YqbPu'></ul>
                  <tfoot id='YqbPu'></tfoot>
                    <tbody id='YqbPu'></tbody>
                • <legend id='YqbPu'><style id='YqbPu'><dir id='YqbPu'><q id='YqbPu'></q></dir></style></legend>
                • <small id='YqbPu'></small><noframes id='YqbPu'>

                  本文介绍了具有扩展选择模式的 WPF 列表框上的取消选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个带有扩展选择模式的简单列表框.选择工作几乎完美无缺,就像它在资源管理器中工作一样.但是取消选择并不是真的那么好.我想要的是,当我单击列表框中元素范围之外的内容时,我希望取消选择所有元素.默认情况下,我似乎并没有那样做,我做了一个肮脏的黑客,涉及 selectionchanged 和 mouseup 来破解它.但必须有更好的方法.有什么想法吗?

                  I have a simple listbox with extended selection mode. Selection works almost perfectly fine like it works in explorer. But deselection doesn't really work all that well. What I want is that when I click on something outside the range of elements in the listbox I want all elements to be deselected. I doesn't seem to behave that way by default and I did a dirty hack involving selectionchanged and mouseup to hack it up. But there has to be a better way. Any ideas?

                  推荐答案

                  添加取消选择功能并没有那么脏,而且您走在正确的轨道上.主要问题是,默认情况下,ListBox 内的 ListBoxItems 会一直延伸,使得点击一个非常困难.

                  It isn't that dirty to add in the deselection functionality, and you're on the right track. The main issue is that by default the ListBoxItems inside the ListBox will stretch all the way across, making it pretty tough to not click on one.

                  这是一个示例 ListBox,它修改了默认的 ItemContainerStyle,使项目仅占据列表的左侧,并且项目之间也有一些间距.

                  Here's an example ListBox that modifies the default ItemContainerStyle so that the items just take up the left side of the list and there is some spacing between the items as well.

                  <ListBox SelectionMode="Extended"
                           Width="200" Mouse.MouseDown="ListBox_MouseDown">
                      <ListBox.ItemContainerStyle>
                          <Style TargetType="{x:Type ListBoxItem}">
                              <Setter Property="Background"
                                      Value="LightBlue" />
                              <Setter Property="Margin"
                                      Value="2" />
                              <Setter Property="Padding"
                                      Value="2" />
                              <Setter Property="Width"
                                      Value="100" />
                              <Setter Property="HorizontalAlignment"
                                      Value="Left" />
                          </Style>
                      </ListBox.ItemContainerStyle>
                      <ListBoxItem >Item 1</ListBoxItem>
                      <ListBoxItem >Item 2</ListBoxItem>
                      <ListBoxItem >Item 3</ListBoxItem>
                      <ListBoxItem >Item 4</ListBoxItem>
                  </ListBox>
                  

                  要取消选择选定的项目,我们只需在 EventHandler 中将 SelectedItem 设置为 null.当我们点击一个 ListBoxItem 时,它会处理 MouseDown/Click 等来设置 SelectedItem 或修改 SelectedItems.正因为如此,以及 RoutedEvents 的性质,我们只是在需要的时候准确地处理 ListBox 中的 MouseDown.当单击 ListBox 内不属于某个项目的某个位置时.

                  To deselect the selected items we just need to set the SelectedItem to null in the EventHandler. When we click on a ListBoxItem, it will handle the MouseDown/Click etc to set the SelectedItem or modify the SelectedItems. Because of this, and the nature of the RoutedEvents we just handle the MouseDown in the ListBox exactly when we want. When somewhere inside the ListBox is clicked that isn't part of an item.

                  private void ListBox_MouseDown(object sender, MouseButtonEventArgs e)
                  {
                      (sender as ListBox).SelectedItem = null;
                  }
                  

                  这篇关于具有扩展选择模式的 WPF 列表框上的取消选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何获取列表框项目的“键"?在 c# winforms 应用程序中? 下一篇:使用 MVVM 在 Windows 手机上从列表拖放到画布

                  相关文章

                • <small id='C3zO8'></small><noframes id='C3zO8'>

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

                      • <bdo id='C3zO8'></bdo><ul id='C3zO8'></ul>
                      <tfoot id='C3zO8'></tfoot>
                      <legend id='C3zO8'><style id='C3zO8'><dir id='C3zO8'><q id='C3zO8'></q></dir></style></legend>