SegmentedButton
Wisej.NET 4.2.0+
A SegmentedButton is a short row of options joined into one control, with one or several of them selected: a time range (1H, 24H, 7D), a view switch (Day, Week, Month), a status filter. It does the job of a group of RadioButton controls, or of CheckBox controls in multiple-selection mode, in a fraction of the space and without a GroupBox around them.
ChipGroup derives from it and shares the same items and selection API, but lays the options out as wrapping chips that can show a count and a close button. Use it for filters a user builds up and takes apart, such as regions, tags or categories, where a fixed row of segments would run out of width.
For a complete list of properties, methods, and events, see the API documentation of SegmentedButton and ChipGroup.
Features
Segments
The options are Segment objects in the Items collection, in display order. A segment carries a Text, an optional Icon (an image URL or a theme image name), a ToolTipText, an Enabled flag and a Value. Value is your application data: it stays on the server and is never sent to the browser, so it can safely hold a key, an enum or an object. Changing a property of a segment that is already in the collection updates the widget.
this.segmentedButton1.Items.Add(new Segment { Text = "Open", Value = OrderStatus.Open });
this.segmentedButton1.Items.Add(new Segment { Text = "Shipped", Value = OrderStatus.Shipped });
this.segmentedButton1.Items.Add(new Segment { Text = "Delivered", Value = OrderStatus.Delivered });
this.segmentedButton1.Items.Add(new Segment { Text = "Returned", Value = OrderStatus.Returned, Enabled = false });
Single and Multiple Selection
SelectionMode is SegmentSelectionMode.One (the default) or SegmentSelectionMode.Multiple. Nothing is selected until you or the user select something.
SelectedIndexandSelectedItemread the first selected segment and, when assigned, make it the only one, even inMultiplemode.-1ornullclears the selection.SelectedIndicesandSelectedItemsreturn every selected segment in collection order.SetSelected(index, value)adds or removes a single selection without touching the others.AllowDeselectdecides whether a click on the last selected segment may clear it. It constrains the user only: code can always clear the selection.
Selection follows the segment, not its position: inserting or moving items keeps the same segments selected, and removing a selected segment drops it from the selection. A disabled segment keeps its selection and can still be selected from code.
Appearance and Size
Appearance picks one of three presentations of SegmentAppearance: Raised (the selected segment lifts off a muted track), Outlined (bordered segments with a solid selected background) and Pill (rounded segments on a dark pill-shaped track). SegmentSize sets the padding and minimum height of the segments, Small (26 pixels), Medium (32) or Large (40), independently of the control's Size. Stretch makes the segments share the full width of the control instead of sizing to their text.
Chips
ChipGroup adds three things to the shared API. ShowCounts displays each segment's Count next to its text; a segment whose Count is null shows none. Closable gives every enabled chip its own close button; closing a chip removes the segment from Items, updates the selection, and then raises ChipClosed. And Stretch, on a chip group, puts each chip on a row of its own at full width. Appearance and SegmentSize style each chip as they style a segment.
Keyboard and Accessibility
Each segment is a button with a tab stop of its own, announced as a radio button in One mode and as a check box in Multiple mode. In a ChipGroup the close button is a separate tab stop, so a keyboard user can select a chip or remove it.
How To
React to a Selection
SelectionChanged is raised whenever the set of selected segments changes, from code or from the user; SelectedIndexChanged only when the first selected index changes. ItemClick is raised after a user click, once the selection events have run, and carries a SegmentEventArgs with the Item and its Index.
private void segmentedButton1_SelectionChanged(object sender, EventArgs e)
{
var status = (OrderStatus?)this.segmentedButton1.SelectedItem?.Value;
this.dataGridView1.DataSource = status == null
? this.orders.ToList()
: this.orders.Where(o => o.Status == status).ToList();
}
private void segmentedButton1_ItemClick(object sender, SegmentEventArgs e)
{
AlertBox.Show($"Showing {e.Item.Text} orders");
}
Build a Multiple-Choice Filter
var weekdays = new SegmentedButton
{
SelectionMode = SegmentSelectionMode.Multiple,
Appearance = SegmentAppearance.Outlined,
SegmentSize = SegmentSize.Small,
Stretch = true
};
foreach (var day in new[] { "Mon", "Tue", "Wed", "Thu", "Fri" })
weekdays.Items.Add(new Segment { Text = day, Value = day });
weekdays.SetSelected(0, true);
weekdays.SetSelected(2, true);
// Later: the days a shipment can be delivered.
var days = weekdays.SelectedItems.Select(s => (string)s.Value).ToArray();
Show Removable Filters with Counts
this.chipGroup1.ShowCounts = true;
this.chipGroup1.Closable = true;
this.chipGroup1.SelectionMode = SegmentSelectionMode.Multiple;
this.chipGroup1.Items.Add(new Segment { Text = "Europe", Value = "EU", Count = 18 });
this.chipGroup1.Items.Add(new Segment { Text = "Americas", Value = "AM", Count = 12 });
this.chipGroup1.Items.Add(new Segment { Text = "APAC", Value = "APAC", Count = 7 });
this.chipGroup1.ChipClosed += (s, e) =>
{
// The segment is already gone from Items; e.Index is where it was.
this.activeRegions.Remove((string)e.Item.Value);
RefreshShipments();
};
CloseText overrides the "Close {0}" label of the close buttons, where {0} is the chip's text; leave it null to use the built-in translation for the current culture.
Advanced
JavaScript Widget
| Item | Description |
|---|---|
| Class name | "wisej.web.SegmentedButton", and "wisej.web.ChipGroup" for the chip group. |
| Theme appearance | "segmentedbutton", with the "row" and "option" components; "chipgroup" for the chip group. See Themes. |