Skip to content

Commit 8c5337d

Browse files
committed
Improvements
1 parent 6d78c42 commit 8c5337d

File tree

7 files changed

+120
-167
lines changed

7 files changed

+120
-167
lines changed

components/SegmentedControl/samples/SegmentedControlBasicSample.xaml

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!-- Licensed to the .NET Foundation under one or more agreements. The .NET Foundation licenses this file to you under the MIT license. See the LICENSE file in the project root for more information. -->
1+
<!-- Licensed to the .NET Foundation under one or more agreements. The .NET Foundation licenses this file to you under the MIT license. See the LICENSE file in the project root for more information. -->
22
<Page x:Class="SegmentedControlExperiment.Samples.SegmentedControlBasicSample"
33
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
44
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
@@ -11,19 +11,37 @@
1111
<StackPanel x:Name="Panel"
1212
Spacing="8">
1313
<TextBlock Style="{StaticResource BodyStrongTextBlockStyle}"
14-
Text="Icon + label" />
14+
Text="Icon + content" />
1515
<labs:Segmented HorizontalAlignment="{x:Bind local:SegmentedControlBasicSample.ConvertStringToHorizontalAlignment(Alignment), Mode=OneWay}"
16+
SelectedIndex="0"
1617
SelectionMode="{x:Bind local:SegmentedControlBasicSample.ConvertStringToSelectionMode(SelectionMode), Mode=OneWay}">
17-
<labs:SegmentedItem>Item 1</labs:SegmentedItem>
18-
<labs:SegmentedItem>Item 2</labs:SegmentedItem>
19-
<labs:SegmentedItem>Item with long label</labs:SegmentedItem>
20-
<labs:SegmentedItem>Item 4</labs:SegmentedItem>
18+
<labs:SegmentedItem Content="Item 1">
19+
<labs:SegmentedItem.Icon>
20+
<FontIcon Glyph="&#xEA3A;" />
21+
</labs:SegmentedItem.Icon>
22+
</labs:SegmentedItem>
23+
<labs:SegmentedItem Content="Item 2">
24+
<labs:SegmentedItem.Icon>
25+
<FontIcon Glyph="&#xEA3A;" />
26+
</labs:SegmentedItem.Icon>
27+
</labs:SegmentedItem>
28+
<labs:SegmentedItem Content="Item 3 with a long label">
29+
<labs:SegmentedItem.Icon>
30+
<FontIcon Glyph="&#xEA3A;" />
31+
</labs:SegmentedItem.Icon>
32+
</labs:SegmentedItem>
33+
<labs:SegmentedItem Content="Item 4">
34+
<labs:SegmentedItem.Icon>
35+
<FontIcon Glyph="&#xEA3A;" />
36+
</labs:SegmentedItem.Icon>
37+
</labs:SegmentedItem>
2138
</labs:Segmented>
2239

2340
<TextBlock Margin="0,24,0,0"
2441
Style="{StaticResource BodyStrongTextBlockStyle}"
2542
Text="Icon only" />
2643
<labs:Segmented HorizontalAlignment="{x:Bind local:SegmentedControlBasicSample.ConvertStringToHorizontalAlignment(Alignment), Mode=OneWay}"
44+
SelectedIndex="2"
2745
SelectionMode="{x:Bind local:SegmentedControlBasicSample.ConvertStringToSelectionMode(SelectionMode), Mode=OneWay}">
2846
<labs:SegmentedItem ToolTipService.ToolTip="Day view">
2947
<labs:SegmentedItem.Icon>

components/SegmentedControl/samples/SegmentedControlStylesSample.xaml

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!-- Licensed to the .NET Foundation under one or more agreements. The .NET Foundation licenses this file to you under the MIT license. See the LICENSE file in the project root for more information. -->
1+
<!-- Licensed to the .NET Foundation under one or more agreements. The .NET Foundation licenses this file to you under the MIT license. See the LICENSE file in the project root for more information. -->
22
<Page x:Class="SegmentedControlExperiment.Samples.SegmentedControlStylesSample"
33
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
44
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
@@ -16,17 +16,22 @@
1616
</Page.Resources>
1717
<StackPanel Spacing="8">
1818
<TextBlock Style="{StaticResource BodyStrongTextBlockStyle}"
19-
Text="Icon + label" />
20-
<labs:Segmented SelectionMode="{x:Bind local:SegmentedControlStylesSample.ConvertStringToSelectionMode(SelectionMode), Mode=OneWay}"
21-
Style="{StaticResource PillSegmentedStyle}">
19+
Text="PivotSegmentedStyle" />
20+
<labs:Segmented SelectedIndex="1"
21+
SelectionMode="{x:Bind local:SegmentedControlStylesSample.ConvertStringToSelectionMode(SelectionMode), Mode=OneWay}"
22+
Style="{StaticResource PivotSegmentedStyle}">
2223
<labs:SegmentedItem>Item 1</labs:SegmentedItem>
2324
<labs:SegmentedItem>Item 2</labs:SegmentedItem>
2425
<labs:SegmentedItem>Item with long label</labs:SegmentedItem>
2526
<labs:SegmentedItem>Item 4</labs:SegmentedItem>
2627
</labs:Segmented>
2728

28-
<labs:Segmented SelectionMode="{x:Bind local:SegmentedControlStylesSample.ConvertStringToSelectionMode(SelectionMode), Mode=OneWay}"
29-
Style="{StaticResource PillSegmentedStyle}">
29+
<TextBlock Margin="0,24,0,0"
30+
Style="{StaticResource BodyStrongTextBlockStyle}"
31+
Text="PivotSegmentedStyle" />
32+
<labs:Segmented SelectedIndex="0"
33+
SelectionMode="{x:Bind local:SegmentedControlStylesSample.ConvertStringToSelectionMode(SelectionMode), Mode=OneWay}"
34+
Style="{StaticResource PivotSegmentedStyle}">
3035
<labs:SegmentedItem ToolTipService.ToolTip="Day view">
3136
<labs:SegmentedItem.Icon>
3237
<FontIcon Glyph="&#xE161;" />

components/SegmentedControl/src/EqualPanel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public double Spacing
2727

2828
public EqualPanel()
2929
{
30-
RegisterPropertyChangedCallback(Panel.HorizontalAlignmentProperty, OnHorizontalAlignmentChanged);
30+
RegisterPropertyChangedCallback(HorizontalAlignmentProperty, OnHorizontalAlignmentChanged);
3131
}
3232

3333
protected override Size MeasureOverride(Size availableSize)

components/SegmentedControl/src/Segmented/Segmented.Properties.cs

Lines changed: 0 additions & 28 deletions
This file was deleted.

components/SegmentedControl/src/Segmented/Segmented.cs

Lines changed: 9 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,14 @@ namespace CommunityToolkit.Labs.WinUI;
88

99
public partial class Segmented : ListViewBase
1010
{
11+
private int _internalSelectedIndex = -1;
1112
public Segmented()
1213
{
1314
this.DefaultStyleKey = typeof(Segmented);
1415

15-
RegisterPropertyChangedCallback(Segmented.SelectionModeProperty, OnSelectionModeChanged);
16-
#if !HAS_UNO
17-
ItemContainerGenerator.ItemsChanged += ItemContainerGenerator_ItemsChanged;
18-
#endif
16+
RegisterPropertyChangedCallback(SelectedIndexProperty, OnSelectedIndexChanged);
1917
}
2018

21-
#if !HAS_UNO
22-
private void ItemContainerGenerator_ItemsChanged(object sender, ItemsChangedEventArgs e)
23-
{
24-
var action = (CollectionChange)e.Action;
25-
if (action == CollectionChange.Reset)
26-
{
27-
// Reset collection to reload later.
28-
_hasLoaded = false;
29-
}
30-
}
31-
#endif
32-
3319
protected override DependencyObject GetContainerForItemOverride() => new SegmentedItem();
3420

3521
protected override bool IsItemItsOwnContainerOverride(object item)
@@ -40,6 +26,7 @@ protected override bool IsItemItsOwnContainerOverride(object item)
4026
protected override void OnApplyTemplate()
4127
{
4228
base.OnApplyTemplate();
29+
SelectedIndex = _internalSelectedIndex;
4330
PreviewKeyDown -= Segmented_PreviewKeyDown;
4431
PreviewKeyDown += Segmented_PreviewKeyDown;
4532
}
@@ -62,54 +49,19 @@ private void Segmented_PreviewKeyDown(object sender, KeyRoutedEventArgs e)
6249
}
6350
}
6451

65-
private bool _hasLoaded;
6652
private void SegmentedItem_Loaded(object sender, RoutedEventArgs e)
6753
{
6854
if (sender is SegmentedItem segmentedItem)
6955
{
7056
segmentedItem.Loaded -= SegmentedItem_Loaded;
7157
}
72-
73-
//// Only need to do this once.
74-
if (!_hasLoaded)
75-
{
76-
_hasLoaded = true;
77-
// Need to set a the selection on load, otherwise ListView resets to null.
78-
SetInitialSelection();
79-
}
8058
}
8159

8260
protected override void OnItemsChanged(object e)
8361
{
84-
IVectorChangedEventArgs args = (IVectorChangedEventArgs)e;
8562
base.OnItemsChanged(e);
8663
}
8764

88-
private void SetInitialSelection()
89-
{
90-
if (SelectedItem == null)
91-
{
92-
// If we have an index, but didn't get the selection, make the selection
93-
if (SelectedIndex >= 0 && SelectedIndex < Items.Count)
94-
{
95-
SelectedItem = Items[SelectedIndex];
96-
}
97-
98-
// Otherwise, select the first item by default
99-
else if (Items.Count >= 1 && SelectionMode == ListViewSelectionMode.Single && AutoSelection)
100-
{
101-
SelectedItem = Items[0];
102-
}
103-
}
104-
else
105-
{
106-
if (SelectedIndex >= 0 && SelectedIndex < Items.Count)
107-
{
108-
SelectedItem = Items[SelectedIndex];
109-
}
110-
}
111-
}
112-
11365
private enum MoveDirection
11466
{
11567
Next,
@@ -175,8 +127,12 @@ private bool MoveFocus(MoveDirection direction)
175127
}
176128
}
177129

178-
private void OnSelectionModeChanged(DependencyObject sender, DependencyProperty dp)
130+
private void OnSelectedIndexChanged(DependencyObject sender, DependencyProperty dp)
179131
{
180-
SetInitialSelection();
132+
// This is a workaround for https://github.com/microsoft/microsoft-ui-xaml/issues/8257
133+
if (_internalSelectedIndex == -1 && SelectedIndex > -1)
134+
{
135+
_internalSelectedIndex = SelectedIndex;
136+
}
181137
}
182138
}

components/SegmentedControl/src/Segmented/Segmented.xaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,15 @@
7676
</Style.Setters>
7777
</Style>
7878

79-
<Style x:Key="PillSegmentedStyle"
79+
<Style x:Key="PivotSegmentedStyle"
8080
BasedOn="{StaticResource DefaultSegmentedStyle}"
8181
TargetType="labs:Segmented">
8282
<Style.Setters>
8383
<Setter Property="Background" Value="Transparent" />
8484
<Setter Property="BorderBrush" Value="Transparent" />
8585
<Setter Property="BorderThickness" Value="0" />
8686
<Setter Property="Padding" Value="0" />
87-
<Setter Property="ItemContainerStyle" Value="{StaticResource PillSegmentedItemStyle}" />
87+
<Setter Property="ItemContainerStyle" Value="{StaticResource PivotSegmentedItemStyle}" />
8888
<Setter Property="ItemsPanel">
8989
<Setter.Value>
9090
<ItemsPanelTemplate>

0 commit comments

Comments
 (0)