|
| 1 | +--- |
| 2 | +title: Read-Only ListBox |
| 3 | +description: Learn how to achieve a read-only effect for the ListBox component in UI for Blazor using event handlers and toolbar visibility settings. |
| 4 | +type: how-to |
| 5 | +page_title: How to Achieve Read-Only Mode for ListBox in Blazor |
| 6 | +meta_title: How to Achieve Read-Only Mode for ListBox in Blazor |
| 7 | +slug: listbox-kb-readonly |
| 8 | +tags: listbox, blazor, readonly |
| 9 | +res_type: kb |
| 10 | +ticketid: 1708525 |
| 11 | +--- |
| 12 | + |
| 13 | +## Environment |
| 14 | +<table> |
| 15 | + <tbody> |
| 16 | + <tr> |
| 17 | + <td>Product</td> |
| 18 | + <td>ListBox for Blazor</td> |
| 19 | + </tr> |
| 20 | + </tbody> |
| 21 | +</table> |
| 22 | + |
| 23 | +## Description |
| 24 | + |
| 25 | +I want to make the [ListBox](slug:listbox-overview) read-only. I am looking for a solution that doesn't allow users to modify the selected items or interact with the toolbar. |
| 26 | + |
| 27 | +## Solution |
| 28 | + |
| 29 | +To achieve a read-only effect in the TelerikListBox, follow these steps: |
| 30 | + |
| 31 | +1. Use one-way binding for the `SelectedItems` parameter of the component. |
| 32 | +2. Define a [`SelectedItemsChanged` event](slug:listbox-events#selecteditemschanged) handler, but avoid updating the `SelectedItems` parameter in the handler. |
| 33 | +3. Hide the ListBox toolbar by setting the `Visible` property of the `ListBoxToolBar` to `false`. |
| 34 | + |
| 35 | +Here is an example: |
| 36 | + |
| 37 | +````RAZOR |
| 38 | +<TelerikListBox Data="@ListBoxStrings" |
| 39 | + SelectedItems="@ListBoxSelectedStrings" |
| 40 | + SelectedItemsChanged="@((IEnumerable<string> list) => ListBoxSelectedStringsHandler(list))" |
| 41 | + Height="auto"> |
| 42 | + <ListBoxToolBarSettings> |
| 43 | + <ListBoxToolBar Visible="false" /> |
| 44 | + </ListBoxToolBarSettings> |
| 45 | +</TelerikListBox> |
| 46 | +
|
| 47 | +@code { |
| 48 | + private List<string> ListBoxStrings { get; set; } = new List<string>(); |
| 49 | + private IEnumerable<string> ListBoxSelectedStrings { get; set; } = new List<string>(); |
| 50 | +
|
| 51 | + private void ListBoxSelectedStringsHandler(IEnumerable<string> list) |
| 52 | + { |
| 53 | + // You can execute custom logic here, but do not update ListBoxSelectedStrings |
| 54 | + // to prevent the user from changing the selection. |
| 55 | + } |
| 56 | +
|
| 57 | + protected override void OnInitialized() |
| 58 | + { |
| 59 | + for (int i = 1; i <= 7; i++) |
| 60 | + { |
| 61 | + ListBoxStrings.Add($"String {i}"); |
| 62 | + } |
| 63 | +
|
| 64 | + ListBoxSelectedStrings = new List<string> { "String 2", "String 5" }; |
| 65 | + } |
| 66 | +} |
| 67 | +```` |
| 68 | + |
| 69 | +## See Also |
| 70 | + |
| 71 | +* [TelerikListBox Overview](slug:listbox-overview) |
| 72 | +* [TelerikListBox Events](slug:listbox-events) |
| 73 | +* [TelerikListBox Toolbar](slug:listbox-toolbar) |
0 commit comments