Could you provide an example where it is valid to have a ListBox that you can not select from? Since the main behavior is to select items. I would probably chose another way to display it.(This is not me trying to be a critic but rather an genuine interest in where this might occur)
Marthin
3
@Martin: for instance if you wanted to drag content from a listboxitem - in this case you're probably not interested in selecting that item. ALSO: when dragging an item: selected item of listbox changes while you drag within the listbox - see this post stackoverflow.com/questions/7589142/…
Danield
1
I believe the reason that Shimmy wants to use ListBox is that the asker can make the listbox selectable sometime. The question is also valuable to me. Say you are building a playing card game. You can select one card from your cards, sometimes, you can select multiple and at other times, you cannot select any.
Gqqnbig
1
Plus, sometimes you have 10 cards and only 4 of them are selectable. Among the 4, you can select up to 3.
Gqqnbig
1
@Marthin: When you have a GridView in a ListBox. Gridview headers provide a lot of functionality that's not available elsewhere. And you have edit controls in the cells of the gridview.
Robin Davies
Respostas:
264
Approach 1 - ItemsControl
Unless you need other aspects of the ListBox, you could use ItemsControl instead. It places items in the ItemsPanel and doesn't have the concept of selection.
<ItemsControlItemsSource="{Binding MyItems}"/>
By default, ItemsControl doesn't support virtualization of its child elements. If you have a lot of items, virtualization can reduce memory usage and improve performance, in which case you could use approach 2 and style the ListBox, or add virtualisation to your ItemsControl.
Approach 2 - Styling ListBox
Alternatively, just style the ListBox such that the selection is not visible.
<ListBox.Resources><StyleTargetType="ListBoxItem"><Style.Resources><!-- SelectedItem with focus --><SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}"
Color="Transparent"/><!-- SelectedItem without focus --><SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}"
Color="Transparent"/><!-- SelectedItem text foreground --><SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}"
Color="Black"/></Style.Resources><SetterProperty="FocusVisualStyle"Value="{x:Null}"/></Style></ListBox.Resources>
no, it will only change the visual effect, not the actual selection behavior
Thomas Levesque
8
My first suggestion was to use ItemsControl. Did you miss that? :)
Drew Noakes
5
Re-reading these comments again I want to point out that @Thomas Levesque's comment is only true of the second approach I show. Using plain ItemsControl will completely remove any concept of selection.
Drew Noakes
1
ItemsControl solution remove out the box scrolling support (scrollbar and mousewheel).
MuiBienCarlota
1
+1 for Approach 1 - ItemsControl. If we have a huge page which we have to scroll, if the user mouses over a ListBox, it effectively disables the MouseWheel as the listbox grabs the MouseWheel events. This means that the user gets frustrated that the mousewheel used to scroll the entire page will randomly stop working, depending on whether the mouse is over a listbox or not.
Contango
159
I found a very simple and straight forward solution working for me, I hope it would do for you as well
This is perfect. it prevent selected item and other controls like buttons still works. exactly what i was looking for
Franck
1
+1 for this approach. If we have a huge page which we have to scroll, if the user mouses over a ListBox, it effectively disables the MouseWheel as the listbox grabs the MouseWheel events. This means that the user gets frustrated that the mousewheel used to scroll the entire page will randomly stop working, depending on whether the mouse is over a listbox or not.
Contango
Excellent. Similar approach also worked for me when I needed buttons on items not to cause item selection - but only if other area of the item was clicked. Simply set the buttons Focusable = "False"!
Jony Adamit
1
Add this additional property to remove the mouseover highlighting as well: <Setter Property="IsHitTestVisible" Value="False" />
DonBoitnott
25
You could switch to using an ItemsControl instead of a ListBox. An ItemsControl has no concept of selection, so there's nothing to turn off.
If you don't want the text to be grey you can specify the disabled color by adding a brush to the style's resources with the following key: {x:Static SystemColors.GrayTextBrushKey}. The other solution would be to override the ListBoxItem control template.
Simples e funcional, obrigado! E é aplicável no WP 8.1 Runtime também.
Malutek
8
Isso também funcionará, se eu precisar usar a caixa de listagem em vez do itemscontrol, mas apenas exibir os itens que não devem ser selecionáveis, eu uso:
Respostas bastante boas aqui, mas eu estava procurando por algo um pouco diferente: quero a seleção, mas simplesmente não quero que ela seja mostrada (ou mostrada em um assunto diferente).
As soluções acima não funcionaram para mim (completamente), então fiz outra coisa: usei um novo estilo para minha caixa de listagem, que redefine completamente os modelos:
In my case, I don't want to disable user interaction with the contents of my ListBoxItems so the solution to set IsEnabled won't work for me.
The other solution that attempts to re-style the ListBoxItem by overriding the color-related properties only works for those instances where you're sure the template uses those properties. That's fine for default styles, but breaks with custom styles.
The solutions that use an ItemsControl breaks too many other things as the ItemsControl has a completely different look than a standard ListBox and doesn't support virtualization, meaning you have to re-template the ItemsPanel anyway.
The above doesn't change the default look of the ListBox, doesn't disable items in the data templates for the ListBox, supports virtualization by default, and works independently of whatever styles may or may not be in use in your app. It's the KISS principle.
Note: This solution does not disable selection by keyboard navigation or right clicking (ie. arrow keys followed by space key)
All previous answers either remove the ability select completly (no switching in runtime) or simply remove the visual effect, but not the selection.
But what if you want to be able to select and show selection by code, but not by user input? May be you want to "freeze" the user's selection while not disabling the whole Listbox?
The solution is to wrap the whole ItemsContentTemplate into a Button that has no visual chrome. The size of the button must be equal to the size of the Item, so it's completely covered.
Now use the button's IsEnabled-Property:
Enable the button to "freeze" the item's Selection-state. This works because the enabled button eats all mouse events before they bubble up to the ListboxItem-Eventhandler. Your ItemsDataTemplate will still receive MouseEvents because it's part of the buttons content.
Disable the button to enable changing the selection by clicking.
@Shimmy: It is common for trivial answers to be similar. There is no duplication here worthy of any flag. If you have any more questions about this, please ask on Meta Stack Overflow.
0
You can place a Textblock above your listbox, it will not change the look of your application and also it won't allow to select any item.
I found a perfect way.
Set ListBox IsHitTestVisible to false so that user can't mouse hover or scroll down or scroll up.
Capture PreviewGotKeyboardFocus e.Handled = true so that user can's select item by keyboard Tab, Arrow up, Arrow down.
Very help ful for me, I wanted grayed out and disabled!
Martin Robins
-3
To disable one or more options in your listbox/dropdown, you can add the "disabled" attribute as shown below. This prevent the user from selection this option, and it gets a gray overlay.
Respostas:
Approach 1 -
ItemsControl
Unless you need other aspects of the
ListBox
, you could useItemsControl
instead. It places items in theItemsPanel
and doesn't have the concept of selection.By default,
ItemsControl
doesn't support virtualization of its child elements. If you have a lot of items, virtualization can reduce memory usage and improve performance, in which case you could use approach 2 and style theListBox
, or add virtualisation to yourItemsControl
.Approach 2 - Styling
ListBox
Alternatively, just style the ListBox such that the selection is not visible.
fonte
ItemsControl
will completely remove any concept of selection.I found a very simple and straight forward solution working for me, I hope it would do for you as well
fonte
Focusable = "False"
!<Setter Property="IsHitTestVisible" Value="False" />
You could switch to using an
ItemsControl
instead of aListBox
. AnItemsControl
has no concept of selection, so there's nothing to turn off.fonte
ItemTemplate
.Another option worth considering is disabling the ListBoxItems. This can be done by setting the ItemContainerStyle as shown in the following snippet.
If you don't want the text to be grey you can specify the disabled color by adding a brush to the style's resources with the following key: {x:Static SystemColors.GrayTextBrushKey}. The other solution would be to override the ListBoxItem control template.
fonte
Isso também funcionará, se eu precisar usar a caixa de listagem em vez do itemscontrol, mas apenas exibir os itens que não devem ser selecionáveis, eu uso:
fonte
Respostas bastante boas aqui, mas eu estava procurando por algo um pouco diferente: quero a seleção, mas simplesmente não quero que ela seja mostrada (ou mostrada em um assunto diferente).
As soluções acima não funcionaram para mim (completamente), então fiz outra coisa: usei um novo estilo para minha caixa de listagem, que redefine completamente os modelos:
Começando com isso, você pode facilmente adicionar seu próprio destaque de seleção ou deixá-lo assim, se não quiser.
fonte
Embora a resposta de @Drew Noakes seja uma solução rápida para a maioria dos casos, existe um pouco de falha na configuração dos pincéis x: Static.
When you set the x:Static brushes as suggested, all of the children controls within the list box item will inherit this style.
That means that, while this will work for disabling the highlighting of the list box item, it may result in undesired effects for the child controls.
For example, if you had a ComboBox within your ListBoxItem, it would disable the mouse over highlighting within the ComboBox.
Instead, consider setting the VisualStates for the Selected, Unselected, and MouseOver events as covered in the solution mentioned in this stackoverflow thread: Remove Control Highlight From ListBoxItem but not children controls.
-Frinny
fonte
I propose yet another solution. Simply re-template
ListBoxItem
to be nothing more than aContentPresenter
, like so...My reasons for this approach are as follows:
In my case, I don't want to disable user interaction with the contents of my
ListBoxItems
so the solution to setIsEnabled
won't work for me.The other solution that attempts to re-style the
ListBoxItem
by overriding the color-related properties only works for those instances where you're sure the template uses those properties. That's fine for default styles, but breaks with custom styles.The solutions that use an
ItemsControl
breaks too many other things as theItemsControl
has a completely different look than a standardListBox
and doesn't support virtualization, meaning you have to re-template theItemsPanel
anyway.The above doesn't change the default look of the
ListBox
, doesn't disable items in the data templates for theListBox
, supports virtualization by default, and works independently of whatever styles may or may not be in use in your app. It's the KISS principle.fonte
Note: This solution does not disable selection by keyboard navigation or right clicking (ie. arrow keys followed by space key)
All previous answers either remove the ability select completly (no switching in runtime) or simply remove the visual effect, but not the selection.
But what if you want to be able to select and show selection by code, but not by user input? May be you want to "freeze" the user's selection while not disabling the whole Listbox?
The solution is to wrap the whole ItemsContentTemplate into a Button that has no visual chrome. The size of the button must be equal to the size of the Item, so it's completely covered. Now use the button's IsEnabled-Property:
Enable the button to "freeze" the item's Selection-state. This works because the enabled button eats all mouse events before they bubble up to the ListboxItem-Eventhandler. Your ItemsDataTemplate will still receive MouseEvents because it's part of the buttons content.
Disable the button to enable changing the selection by clicking.
dartrax
fonte
Maybe you need onlly functionality of ItemsControl? It don't allow selection:
fonte
You can place a Textblock above your listbox, it will not change the look of your application and also it won't allow to select any item.
fonte
A simple fix that works on Windows Phone for instance is on selection setting selected item to null:
And in the code behind:
fonte
I found a perfect way.
Set ListBox IsHitTestVisible to false so that user can't mouse hover or scroll down or scroll up.
Capture PreviewGotKeyboardFocus e.Handled = true so that user can's select item by keyboard Tab, Arrow up, Arrow down.
This way advantage:
xmal
code
fonte
For me best solution is:
fonte
IsEnabled = false
fonte
To disable one or more options in your listbox/dropdown, you can add the "disabled" attribute as shown below. This prevent the user from selection this option, and it gets a gray overlay.
fonte