Skip to content

Commit 0780dae

Browse files
committed
Add AutoSelection property
1 parent 342284e commit 0780dae

File tree

5 files changed

+59
-20
lines changed

5 files changed

+59
-20
lines changed

components/SegmentedControl/samples/SegmentedControl.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ issue-id: 0
2121

2222
# Segmented control
2323

24-
The `Segmented` control is a control to be used for configuring a view or setting. It's based off `ListViewBase` and support single and multi-selection.
24+
The `Segmented` control is a control to be used for configuring a view or setting.
2525

2626

2727
## The basics
@@ -30,6 +30,9 @@ The `Segmented` control is best used with 2-5 items and does not support overflo
3030

3131
> [!Sample SegmentedControlBasicSample]
3232
33+
## Selection
34+
`Segmented` supports single and multi-selection. When `SelectionMode` is set to `Single` the first item will be selected by default. This can be overriden by settings `AutoSelection` to `false`.
35+
3336
## Other styles
3437

3538
The `Segmented` control contains various additional styles, to match the look and feel of your application. The `PillSegmentedStyle` matches a modern `Pivot` and `NavigationView` style.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using Windows.System;
6+
7+
namespace CommunityToolkit.Labs.WinUI;
8+
9+
public partial class Segmented : ListViewBase
10+
{
11+
/// <summary>
12+
/// The backing <see cref="DependencyProperty"/> for the <see cref="AutoSelection"/> property.
13+
/// </summary>
14+
public static readonly DependencyProperty AutoSelectionProperty = DependencyProperty.Register(
15+
nameof(AutoSelection),
16+
typeof(bool),
17+
typeof(Segmented),
18+
new PropertyMetadata(defaultValue: true));
19+
20+
/// <summary>
21+
/// Gets or sets the icon.
22+
/// </summary>
23+
public bool AutoSelection
24+
{
25+
get => (bool)GetValue(AutoSelectionProperty);
26+
set => SetValue(AutoSelectionProperty, value);
27+
}
28+
}

components/SegmentedControl/src/Segmented/Segmented.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ private void SetInitialSelection()
9797
}
9898

9999
// Otherwise, select the first item by default
100-
else if (Items.Count >= 1 && SelectionMode == ListViewSelectionMode.Single)
100+
else if (Items.Count >= 1 && SelectionMode == ListViewSelectionMode.Single && AutoSelection)
101101
{
102102
SelectedItem = Items[0];
103103
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
namespace CommunityToolkit.Labs.WinUI;
6+
7+
public partial class SegmentedItem : ListViewItem
8+
{
9+
/// <summary>
10+
/// The backing <see cref="DependencyProperty"/> for the <see cref="Icon"/> property.
11+
/// </summary>
12+
public static readonly DependencyProperty IconProperty = DependencyProperty.Register(
13+
nameof(Icon),
14+
typeof(IconElement),
15+
typeof(SegmentedItem),
16+
new PropertyMetadata(defaultValue: null, (d, e) => ((SegmentedItem)d).OnIconPropertyChanged((IconElement)e.OldValue, (IconElement)e.NewValue)));
17+
18+
/// <summary>
19+
/// Gets or sets the icon.
20+
/// </summary>
21+
public IconElement Icon
22+
{
23+
get => (IconElement)GetValue(IconProperty);
24+
set => SetValue(IconProperty, value);
25+
}
26+
}

components/SegmentedControl/src/SegmentedItem/SegmentedItem.cs

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,6 @@ public partial class SegmentedItem : ListViewItem
1111
internal const string IconOnlyState = "IconOnly";
1212
internal const string ContentOnlyState = "ContentOnly";
1313

14-
/// <summary>
15-
/// The backing <see cref="DependencyProperty"/> for the <see cref="Icon"/> property.
16-
/// </summary>
17-
public static readonly DependencyProperty IconProperty = DependencyProperty.Register(
18-
nameof(Icon),
19-
typeof(IconElement),
20-
typeof(SegmentedItem),
21-
new PropertyMetadata(defaultValue: null, (d, e) => ((SegmentedItem)d).OnIconPropertyChanged((IconElement)e.OldValue, (IconElement)e.NewValue)));
22-
23-
/// <summary>
24-
/// Gets or sets the icon.
25-
/// </summary>
26-
public IconElement Icon
27-
{
28-
get => (IconElement)GetValue(IconProperty);
29-
set => SetValue(IconProperty, value);
30-
}
31-
3214
public SegmentedItem()
3315
{
3416
this.DefaultStyleKey = typeof(SegmentedItem);

0 commit comments

Comments
 (0)