This repository was archived by the owner on Feb 25, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathHeaderedContentControl.cs
More file actions
142 lines (125 loc) · 5.1 KB
/
Copy pathHeaderedContentControl.cs
File metadata and controls
142 lines (125 loc) · 5.1 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// 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.
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
namespace CommunityToolkit.WinUI.UI.Controls
{
/// <summary>
/// Provides the base implementation for all controls that contain single content and have a header.
/// </summary>
public partial class HeaderedContentControl : ContentControl
{
private const string PartHeaderPresenter = "HeaderPresenter";
/// <summary>
/// Initializes a new instance of the <see cref="HeaderedContentControl"/> class.
/// </summary>
public HeaderedContentControl()
{
DefaultStyleKey = typeof(HeaderedContentControl);
}
/// <summary>
/// Identifies the <see cref="Header"/> dependency property.
/// </summary>
public static readonly DependencyProperty HeaderProperty = DependencyProperty.Register(
"Header",
typeof(object),
typeof(HeaderedContentControl),
new PropertyMetadata(null, OnHeaderChanged));
/// <summary>
/// Identifies the <see cref="HeaderTemplate"/> dependency property.
/// </summary>
public static readonly DependencyProperty HeaderTemplateProperty = DependencyProperty.Register(
"HeaderTemplate",
typeof(DataTemplate),
typeof(HeaderedContentControl),
new PropertyMetadata(null));
/// <summary>
/// Identifies the <see cref="Orientation"/> dependency property.
/// </summary>
public static readonly DependencyProperty OrientationProperty = DependencyProperty.Register(
nameof(Orientation),
typeof(Orientation),
typeof(HeaderedContentControl),
new PropertyMetadata(Orientation.Vertical, OnOrientationChanged));
/// <summary>
/// Gets or sets the <see cref="Orientation"/> used for the header.
/// </summary>
/// <remarks>
/// If set to <see cref="Orientation.Vertical"/> the header will be above the content.
/// If set to <see cref="Orientation.Horizontal"/> the header will be to the left of the content.
/// </remarks>
public Orientation Orientation
{
get { return (Orientation)GetValue(OrientationProperty); }
set { SetValue(OrientationProperty, value); }
}
/// <summary>
/// Gets or sets the data used for the header of each control.
/// </summary>
public object Header
{
get { return GetValue(HeaderProperty); }
set { SetValue(HeaderProperty, value); }
}
/// <summary>
/// Gets or sets the template used to display the content of the control's header.
/// </summary>
public DataTemplate HeaderTemplate
{
get { return (DataTemplate)GetValue(HeaderTemplateProperty); }
set { SetValue(HeaderTemplateProperty, value); }
}
/// <inheritdoc/>
protected override void OnApplyTemplate()
{
base.OnApplyTemplate();
SetHeaderVisibility();
SetOrientation();
}
/// <summary>
/// Called when the <see cref="Header"/> property changes.
/// </summary>
/// <param name="oldValue">The old value of the <see cref="Header"/> property.</param>
/// <param name="newValue">The new value of the <see cref="Header"/> property.</param>
protected virtual void OnHeaderChanged(object oldValue, object newValue)
{
}
private static void OnOrientationChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var control = (HeaderedContentControl)d;
control.SetOrientation();
}
private static void OnHeaderChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var control = (HeaderedContentControl)d;
control.SetHeaderVisibility();
control.OnHeaderChanged(e.OldValue, e.NewValue);
}
private void SetHeaderVisibility()
{
if (GetTemplateChild(PartHeaderPresenter) is FrameworkElement headerPresenter)
{
if (Header is string headerText)
{
headerPresenter.Visibility = string.IsNullOrEmpty(headerText)
? Visibility.Collapsed
: Visibility.Visible;
}
else
{
headerPresenter.Visibility = Header != null
? Visibility.Visible
: Visibility.Collapsed;
}
}
}
private void SetOrientation()
{
var orientation = this.Orientation == Orientation.Vertical
? nameof(Orientation.Vertical)
: nameof(Orientation.Horizontal);
VisualStateManager.GoToState(this, orientation, true);
}
}
}