forked from CommunityToolkit/Windows
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSegmentedItem.cs
More file actions
72 lines (61 loc) · 2.26 KB
/
Copy pathSegmentedItem.cs
File metadata and controls
72 lines (61 loc) · 2.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// 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.
namespace CommunityToolkit.WinUI.Controls;
/// <summary>
/// Represents an item in a <see cref="Segmented"/> control.
/// </summary>
[ContentProperty(Name = nameof(Content))]
public partial class SegmentedItem : ListViewItem
{
internal const string IconLeftState = "IconLeft";
internal const string IconTopState = "IconTop";
internal const string IconOnlyState = "IconOnly";
internal const string ContentOnlyState = "ContentOnly";
internal const string HorizontalState = "Horizontal";
internal const string VerticalState = "Vertical";
private bool _isVertical = false;
/// <summary>
/// Creates a new instance of <see cref="SegmentedItem"/>.
/// </summary>
public SegmentedItem()
{
this.DefaultStyleKey = typeof(SegmentedItem);
}
/// <inheritdoc/>
protected override void OnApplyTemplate()
{
base.OnApplyTemplate();
UpdateState();
}
/// <summary>
/// Handles changes to the Content property.
/// </summary>
protected override void OnContentChanged(object oldContent, object newContent)
{
base.OnContentChanged(oldContent, newContent);
UpdateState();
}
/// <summary>
/// Handles changes to the Icon property.
/// </summary>
protected virtual void OnIconPropertyChanged(IconElement oldValue, IconElement newValue) => UpdateState();
internal void UpdateOrientation(Orientation orientation)
{
_isVertical = orientation is Orientation.Vertical;
UpdateState();
}
private void UpdateState()
{
string contentState = (Icon is null, Content is null) switch
{
(false, false) => _isVertical ? IconTopState : IconLeftState,
(false, true) => IconOnlyState,
(true, false) => ContentOnlyState,
(true, true) => ContentOnlyState, // Invalid state. Treat as content only
};
// Update states
VisualStateManager.GoToState(this, contentState, true);
VisualStateManager.GoToState(this, _isVertical ? VerticalState : HorizontalState, true);
}
}