Skip to content

Commit 4299b87

Browse files
dimodiDimo Dimov
andauthored
docs: Add KB for dynamic Switch resizing (#542)
Co-authored-by: Dimo Dimov <dimo@Dimos-MacBook-Pro.local>
1 parent ddd2e79 commit 4299b87

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
---
2+
title: Stretch Switch Dynamically to Fit Long Text Labels
3+
description: How to expand the TelerikSwitch automatically, according to its text labels.
4+
type: how-to
5+
page_title: Resize the Switch on the fly, based on longer on/off labels
6+
slug: switch-strecth-expand-resize
7+
position:
8+
tags: switch, stretch, resize, expand, labels
9+
ticketid: 1539464
10+
res_type: kb
11+
---
12+
13+
## Description
14+
15+
How do I stretch the Switch horizontally, in order to see the whole dynamic on/off text labels? The goal is to expand the component and fit the complete text automatically inside the TelerikSwitch dynamically at runtime. Setting the width to a fixed value is not a viable solution.
16+
17+
## Solution
18+
19+
The Switch relies on absolute positioning styles, which require defined dimensions to work properly. However, it is possible to calculate the Switch width dynamically, based on the length of its labels.
20+
21+
>caption Expand the Switch on the fly, depending on the text label length
22+
23+
````CSHTML
24+
<TelerikSwitch Value="@Value"
25+
Width="@Width"
26+
OnLabel="@OnLabel"
27+
OffLabel="@OffLabel"></TelerikSwitch>
28+
29+
@code {
30+
bool Value { get; set; }
31+
string OnLabel { get; set; } = "Long text on";
32+
string OffLabel { get; set; } = "Long text off and some more text";
33+
34+
string Width
35+
{
36+
get
37+
{
38+
return Math.Max(OnLabel.Length, OffLabel.Length) * .8 + "em";
39+
}
40+
}
41+
}
42+
````

0 commit comments

Comments
 (0)