Skip to content

Commit cdf1299

Browse files
authored
Add DrawerHeaderTemplate and DrawerFooterTemplate properties to DrawerPage (#21008)
* Add DrawerHeaderTemplate and DrawerFooterTemplate properties to DrawerPage * Updated sample
1 parent edb89bd commit cdf1299

File tree

6 files changed

+362
-2
lines changed

6 files changed

+362
-2
lines changed

samples/ControlCatalog/Pages/DrawerPage/DrawerPageCustomizationPage.xaml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,14 @@
7272
<ComboBoxItem Content="Purple" />
7373
</ComboBox>
7474

75+
<TextBlock Text="Header Template" FontSize="12" Opacity="0.7" />
76+
<ComboBox x:Name="HeaderTemplateCombo" SelectedIndex="0"
77+
SelectionChanged="OnHeaderTemplateChanged" HorizontalAlignment="Stretch">
78+
<ComboBoxItem Content="None (direct control)" />
79+
<ComboBoxItem Content="Banner" />
80+
<ComboBoxItem Content="Avatar card" />
81+
</ComboBox>
82+
7583
<TextBlock Text="Footer Background" FontSize="12" Opacity="0.7" />
7684
<ComboBox x:Name="FooterBgCombo" SelectedIndex="0"
7785
SelectionChanged="OnFooterBgChanged" HorizontalAlignment="Stretch">
@@ -82,6 +90,14 @@
8290
<ComboBoxItem Content="Maroon" />
8391
</ComboBox>
8492

93+
<TextBlock Text="Footer Template" FontSize="12" Opacity="0.7" />
94+
<ComboBox x:Name="FooterTemplateCombo" SelectedIndex="0"
95+
SelectionChanged="OnFooterTemplateChanged" HorizontalAlignment="Stretch">
96+
<ComboBoxItem Content="None (direct control)" />
97+
<ComboBoxItem Content="Version badge" />
98+
<ComboBoxItem Content="Icon + label" />
99+
</ComboBox>
100+
85101
<TextBlock Text="Drawer Icon" FontSize="12" Opacity="0.7" />
86102
<ComboBox x:Name="IconCombo" SelectedIndex="0"
87103
SelectionChanged="OnIconChanged" HorizontalAlignment="Stretch">

samples/ControlCatalog/Pages/DrawerPage/DrawerPageCustomizationPage.xaml.cs

Lines changed: 123 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
using System.Linq;
22
using Avalonia.Controls;
33
using Avalonia.Controls.Primitives;
4+
using Avalonia.Controls.Templates;
45
using Avalonia.Input.GestureRecognizers;
56
using Avalonia.Interactivity;
7+
using Avalonia.Layout;
68
using Avalonia.Media;
79

810
namespace ControlCatalog.Pages
@@ -164,7 +166,7 @@ private void OnShowHeaderToggled(object? sender, RoutedEventArgs e)
164166
if (!_isLoaded)
165167
return;
166168
if (ShowHeaderCheck.IsChecked == true)
167-
DemoDrawer.DrawerHeader = DrawerHeaderBorder;
169+
DemoDrawer.DrawerHeader = HeaderTemplateCombo.SelectedIndex == 0 ? DrawerHeaderBorder : (object)"My Application";
168170
else
169171
DemoDrawer.DrawerHeader = null;
170172
}
@@ -174,9 +176,128 @@ private void OnShowFooterToggled(object? sender, RoutedEventArgs e)
174176
if (!_isLoaded)
175177
return;
176178
if (ShowFooterCheck.IsChecked == true)
177-
DemoDrawer.DrawerFooter = DrawerFooterBorder;
179+
{
180+
DemoDrawer.DrawerFooter = FooterTemplateCombo.SelectedIndex switch
181+
{
182+
1 => (object)"v12.0",
183+
2 => (object)"Avalonia",
184+
_ => DrawerFooterBorder
185+
};
186+
}
178187
else
188+
{
179189
DemoDrawer.DrawerFooter = null;
190+
}
191+
}
192+
193+
private void OnHeaderTemplateChanged(object? sender, SelectionChangedEventArgs e)
194+
{
195+
if (!_isLoaded)
196+
return;
197+
198+
switch (HeaderTemplateCombo.SelectedIndex)
199+
{
200+
case 1:
201+
DemoDrawer.DrawerHeader = "My Application";
202+
DemoDrawer.DrawerHeaderTemplate = new FuncDataTemplate<string>((data, _) =>
203+
new Border
204+
{
205+
Padding = new Avalonia.Thickness(16),
206+
Child = new StackPanel
207+
{
208+
Spacing = 2,
209+
Children =
210+
{
211+
new TextBlock { Text = data, FontSize = 18, FontWeight = FontWeight.SemiBold, Foreground = Brushes.White },
212+
new TextBlock { Text = "Navigation", FontSize = 12, Foreground = Brushes.White, Opacity = 0.7 }
213+
}
214+
}
215+
});
216+
break;
217+
218+
case 2:
219+
DemoDrawer.DrawerHeader = "My Application";
220+
DemoDrawer.DrawerHeaderTemplate = new FuncDataTemplate<string>((data, _) =>
221+
{
222+
var initial = data?.Length > 0 ? data[0].ToString().ToUpperInvariant() : "?";
223+
var avatar = new Border
224+
{
225+
Width = 40,
226+
Height = 40,
227+
CornerRadius = new Avalonia.CornerRadius(20),
228+
Background = new SolidColorBrush(Color.Parse("#1976D2")),
229+
Child = new TextBlock
230+
{
231+
Text = initial,
232+
FontSize = 18,
233+
FontWeight = FontWeight.Bold,
234+
Foreground = Brushes.White,
235+
HorizontalAlignment = HorizontalAlignment.Center,
236+
VerticalAlignment = VerticalAlignment.Center
237+
}
238+
};
239+
var label = new TextBlock { Text = data, FontSize = 14, FontWeight = FontWeight.SemiBold, VerticalAlignment = VerticalAlignment.Center };
240+
var row = new StackPanel { Orientation = Orientation.Horizontal, Spacing = 10 };
241+
row.Children.Add(avatar);
242+
row.Children.Add(label);
243+
return new Border { Padding = new Avalonia.Thickness(12), Child = row };
244+
});
245+
break;
246+
247+
default:
248+
DemoDrawer.DrawerHeader = DrawerHeaderBorder;
249+
DemoDrawer.DrawerHeaderTemplate = null;
250+
break;
251+
}
252+
}
253+
254+
private void OnFooterTemplateChanged(object? sender, SelectionChangedEventArgs e)
255+
{
256+
if (!_isLoaded)
257+
return;
258+
259+
switch (FooterTemplateCombo.SelectedIndex)
260+
{
261+
case 1:
262+
DemoDrawer.DrawerFooter = "v12.0";
263+
DemoDrawer.DrawerFooterTemplate = new FuncDataTemplate<string>((data, _) =>
264+
new Border
265+
{
266+
Padding = new Avalonia.Thickness(12, 8),
267+
Child = new Border
268+
{
269+
Padding = new Avalonia.Thickness(8, 4),
270+
CornerRadius = new Avalonia.CornerRadius(4),
271+
Background = new SolidColorBrush(Color.Parse("#1976D2")),
272+
Child = new TextBlock { Text = data, FontSize = 11, Foreground = Brushes.White, FontWeight = FontWeight.SemiBold }
273+
}
274+
});
275+
break;
276+
277+
case 2:
278+
DemoDrawer.DrawerFooter = "Avalonia";
279+
DemoDrawer.DrawerFooterTemplate = new FuncDataTemplate<string>((data, _) =>
280+
{
281+
var icon = new PathIcon
282+
{
283+
Width = 14,
284+
Height = 14,
285+
Data = Geometry.Parse("M13,9H11V7H13M13,17H11V11H13M12,2A10,10 0 0,0 2,12A10,10 0 0,0 12,22A10,10 0 0,0 22,12A10,10 0 0,0 12,2Z"),
286+
Opacity = 0.5
287+
};
288+
var label = new TextBlock { Text = data, FontSize = 12, Opacity = 0.6, VerticalAlignment = VerticalAlignment.Center };
289+
var row = new StackPanel { Orientation = Orientation.Horizontal, Spacing = 6 };
290+
row.Children.Add(icon);
291+
row.Children.Add(label);
292+
return new Border { Padding = new Avalonia.Thickness(14, 10), Child = row };
293+
});
294+
break;
295+
296+
default:
297+
DemoDrawer.DrawerFooter = DrawerFooterBorder;
298+
DemoDrawer.DrawerFooterTemplate = null;
299+
break;
300+
}
180301
}
181302

182303
private void OnMenuItemClick(object? sender, RoutedEventArgs e)

src/Avalonia.Controls/Page/DrawerPage.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,18 @@ public class DrawerPage : Page
124124
public static readonly StyledProperty<object?> DrawerFooterProperty =
125125
AvaloniaProperty.Register<DrawerPage, object?>(nameof(DrawerFooter));
126126

127+
/// <summary>
128+
/// Defines the <see cref="DrawerHeaderTemplate"/> property.
129+
/// </summary>
130+
public static readonly StyledProperty<IDataTemplate?> DrawerHeaderTemplateProperty =
131+
AvaloniaProperty.Register<DrawerPage, IDataTemplate?>(nameof(DrawerHeaderTemplate));
132+
133+
/// <summary>
134+
/// Defines the <see cref="DrawerFooterTemplate"/> property.
135+
/// </summary>
136+
public static readonly StyledProperty<IDataTemplate?> DrawerFooterTemplateProperty =
137+
AvaloniaProperty.Register<DrawerPage, IDataTemplate?>(nameof(DrawerFooterTemplate));
138+
127139
/// <summary>
128140
/// Defines the <see cref="DrawerIcon"/> property.
129141
/// </summary>
@@ -403,6 +415,7 @@ public DrawerPlacement DrawerPlacement
403415
/// <summary>
404416
/// Gets or sets the header content displayed at the top of the drawer pane.
405417
/// </summary>
418+
[DependsOn(nameof(DrawerHeaderTemplate))]
406419
public object? DrawerHeader
407420
{
408421
get => GetValue(DrawerHeaderProperty);
@@ -412,12 +425,31 @@ public object? DrawerHeader
412425
/// <summary>
413426
/// Gets or sets the footer content displayed at the bottom of the drawer pane.
414427
/// </summary>
428+
[DependsOn(nameof(DrawerFooterTemplate))]
415429
public object? DrawerFooter
416430
{
417431
get => GetValue(DrawerFooterProperty);
418432
set => SetValue(DrawerFooterProperty, value);
419433
}
420434

435+
/// <summary>
436+
/// Gets or sets the data template used to display <see cref="DrawerHeader"/>.
437+
/// </summary>
438+
public IDataTemplate? DrawerHeaderTemplate
439+
{
440+
get => GetValue(DrawerHeaderTemplateProperty);
441+
set => SetValue(DrawerHeaderTemplateProperty, value);
442+
}
443+
444+
/// <summary>
445+
/// Gets or sets the data template used to display <see cref="DrawerFooter"/>.
446+
/// </summary>
447+
public IDataTemplate? DrawerFooterTemplate
448+
{
449+
get => GetValue(DrawerFooterTemplateProperty);
450+
set => SetValue(DrawerFooterTemplateProperty, value);
451+
}
452+
421453
/// <summary>
422454
/// Gets or sets the icon displayed in the drawer toggle button.
423455
/// </summary>

src/Avalonia.Themes.Fluent/Controls/DrawerPage.xaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,13 @@
5757
<ContentPresenter Name="PART_DrawerHeader"
5858
DockPanel.Dock="Top"
5959
Content="{TemplateBinding DrawerHeader}"
60+
ContentTemplate="{TemplateBinding DrawerHeaderTemplate}"
6061
Background="{TemplateBinding DrawerHeaderBackground}"
6162
IsVisible="{TemplateBinding DrawerHeader, Converter={x:Static ObjectConverters.IsNotNull}}" />
6263
<ContentPresenter Name="PART_DrawerFooter"
6364
DockPanel.Dock="Bottom"
6465
Content="{TemplateBinding DrawerFooter}"
66+
ContentTemplate="{TemplateBinding DrawerFooterTemplate}"
6567
Background="{TemplateBinding DrawerFooterBackground}"
6668
IsVisible="{TemplateBinding DrawerFooter, Converter={x:Static ObjectConverters.IsNotNull}}" />
6769
<ContentPresenter Name="PART_DrawerPresenter"

src/Avalonia.Themes.Simple/Controls/DrawerPage.xaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,13 @@
5656
<ContentPresenter Name="PART_DrawerHeader"
5757
DockPanel.Dock="Top"
5858
Content="{TemplateBinding DrawerHeader}"
59+
ContentTemplate="{TemplateBinding DrawerHeaderTemplate}"
5960
Background="{TemplateBinding DrawerHeaderBackground}"
6061
IsVisible="{TemplateBinding DrawerHeader, Converter={x:Static ObjectConverters.IsNotNull}}" />
6162
<ContentPresenter Name="PART_DrawerFooter"
6263
DockPanel.Dock="Bottom"
6364
Content="{TemplateBinding DrawerFooter}"
65+
ContentTemplate="{TemplateBinding DrawerFooterTemplate}"
6466
Background="{TemplateBinding DrawerFooterBackground}"
6567
IsVisible="{TemplateBinding DrawerFooter, Converter={x:Static ObjectConverters.IsNotNull}}" />
6668
<ContentPresenter Name="PART_DrawerPresenter"

0 commit comments

Comments
 (0)