|
| 1 | +--- |
| 2 | +title: Events |
| 3 | +page_title: Drawer for Blazor | Events |
| 4 | +description: Events in the Drawer for Blazor |
| 5 | +slug: drawer-events |
| 6 | +tags: telerik,blazor,drawer,event,events |
| 7 | +published: True |
| 8 | +position: 25 |
| 9 | +--- |
| 10 | + |
| 11 | +# Drawer Events |
| 12 | + |
| 13 | +This article explains the events available in the Telerik Drawer for Blazor: |
| 14 | + |
| 15 | +* [SelectedItemChanged](#selecteditemchanged) |
| 16 | +* [ExpandedChanged](#expandedchanged) |
| 17 | + |
| 18 | + |
| 19 | +## SelectedItemChanged |
| 20 | + |
| 21 | +The `SelectedItemChanged` event fires every time the user clicks on a new item from the Drawer. You can use it with one-way data binding to respond to the user [selection]({%slug drawer-selection%}). It receives an argument of the Drawer data model type. |
| 22 | + |
| 23 | +>caption Handle SelectedItemChanged event |
| 24 | +
|
| 25 | +````CSHTML |
| 26 | +@* This example shows how to use one-way data binding for the SelectedItem parameter *@ |
| 27 | +
|
| 28 | +<TelerikDrawer Data="@Data" Expanded="true" MiniMode="true" Mode="DrawerMode.Push" |
| 29 | + SelectedItem="@selectedItem" |
| 30 | + SelectedItemChanged="((DrawerItem item) => SelectedItemChangedHandler(item))"> |
| 31 | + <Content> |
| 32 | + <div class="text-info"> |
| 33 | + Content for the @selectedItem?.Text |
| 34 | + </div> |
| 35 | + </Content> |
| 36 | +</TelerikDrawer> |
| 37 | +
|
| 38 | +@code { |
| 39 | + private void SelectedItemChangedHandler(DrawerItem item) |
| 40 | + { |
| 41 | + selectedItem = item; |
| 42 | + // if you don't update the view-model, the event will effectively be cancelled |
| 43 | +
|
| 44 | + Console.WriteLine($"The user selected {item.Text}"); |
| 45 | + } |
| 46 | +
|
| 47 | + public DrawerItem selectedItem { get; set; } |
| 48 | + public IEnumerable<DrawerItem> Data { get; set; } = |
| 49 | + new List<DrawerItem> |
| 50 | + { |
| 51 | + new DrawerItem { Text = "Counter", Icon = IconName.Plus}, |
| 52 | + new DrawerItem { Text = "FetchData", Icon = IconName.GridLayout}, |
| 53 | + }; |
| 54 | +
|
| 55 | + public class DrawerItem |
| 56 | + { |
| 57 | + public string Text { get; set; } |
| 58 | + public string Icon { get; set; } |
| 59 | + } |
| 60 | +} |
| 61 | +```` |
| 62 | + |
| 63 | + |
| 64 | +## ExpandedChanged |
| 65 | + |
| 66 | +The `ExpandedChanged` event fires every time the component's state is changed - to expanded or to collapsed. You can use it with one-way data binding for the `Expanded` parameter. It takes an argument of the `bool` type that corresponds to its new state - whether the drawer is expanded. |
| 67 | + |
| 68 | +>tip If you only need conditional markup based on the expanded/collapsed state of the drawer, use two-way binding (`@bind-Expanded`) - in this example, hiding the button conditionally can be achieved either way, but two-way binding requires less code. |
| 69 | +
|
| 70 | +>caption Handle ExpandedChanged event |
| 71 | +
|
| 72 | +````CSHTML |
| 73 | +@* This example shows how to use one-way data binding for the Expanded parameter and show/hide the Expand Drawer button based on the value of Expanded *@ |
| 74 | +
|
| 75 | +@if (!Expanded) |
| 76 | +{ |
| 77 | + <TelerikButton OnClick="@(() => DrawerRef.ExpandAsync())" Icon="@IconName.Menu">Expand Drawer</TelerikButton> |
| 78 | +} |
| 79 | +
|
| 80 | +<TelerikDrawer Expanded="@Expanded" |
| 81 | + ExpandedChanged="((bool newValue) => ExpandedChangedHandler(newValue))" |
| 82 | + Data="@Data" |
| 83 | + MiniMode="true" |
| 84 | + Mode="@DrawerMode.Push" |
| 85 | + @bind-SelectedItem="@selectedItem" |
| 86 | + @ref="@DrawerRef"> |
| 87 | + <Content> |
| 88 | + <div class="text-info"> |
| 89 | + Content for the @selectedItem?.Text |
| 90 | + </div> |
| 91 | + </Content> |
| 92 | +</TelerikDrawer> |
| 93 | +
|
| 94 | +@code { |
| 95 | + private void ExpandedChangedHandler(bool value) |
| 96 | + { |
| 97 | + Expanded = value; |
| 98 | + // if you don't update the view-model, the event will be effectively cancelled |
| 99 | +
|
| 100 | + Console.WriteLine(string.Format("the user {0} the drawer.", Expanded ? "expanded" : "collapsed")); |
| 101 | + } |
| 102 | +
|
| 103 | + public TelerikDrawer<DrawerItem> DrawerRef { get; set; } |
| 104 | + public DrawerItem selectedItem { get; set; } |
| 105 | + public bool Expanded { get; set; } = true; |
| 106 | + public IEnumerable<DrawerItem> Data { get; set; } = |
| 107 | + new List<DrawerItem> |
| 108 | + { |
| 109 | + new DrawerItem { Text = "Counter", Icon = IconName.Plus}, |
| 110 | + new DrawerItem { Text = "FetchData", Icon = IconName.GridLayout}, |
| 111 | + }; |
| 112 | +
|
| 113 | + public class DrawerItem |
| 114 | + { |
| 115 | + public string Text { get; set; } |
| 116 | + public string Icon { get; set; } |
| 117 | + } |
| 118 | +} |
| 119 | +```` |
| 120 | +>caption The result from the code snippet above |
| 121 | +
|
| 122 | + |
| 123 | + |
| 124 | +@[template](/_contentTemplates/common/general-info.md#event-callback-can-be-async) |
| 125 | + |
| 126 | +@[template](/_contentTemplates/common/issues-and-warnings.md#valuechanged-lambda-required) |
0 commit comments