Skip to content

Commit 961bd13

Browse files
committed
[NAOT] Fix SidebarView Bindings and Support NAOT
1 parent 3c14a16 commit 961bd13

10 files changed

Lines changed: 103 additions & 80 deletions

File tree

dev/DevWinUI.Controls/Controls/Ported/Sidebar/SidebarItem.Properties.cs

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,53 @@ public static FrameworkElement GetTemplateRoot(DependencyObject target)
9797
public static readonly DependencyProperty TemplateRootProperty =
9898
DependencyProperty.Register("TemplateRoot", typeof(FrameworkElement), typeof(FrameworkElement), new PropertyMetadata(null));
9999

100-
public static void OnPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
100+
internal string TextValue
101+
{
102+
get => (string)GetValue(TextValueProperty);
103+
set => SetValue(TextValueProperty, value);
104+
}
105+
public static readonly DependencyProperty TextValueProperty =
106+
DependencyProperty.Register(nameof(TextValue), typeof(string), typeof(SidebarItem), new PropertyMetadata(""));
107+
108+
internal object? ToolTipValue
109+
{
110+
get => GetValue(ToolTipValueProperty);
111+
set => SetValue(ToolTipValueProperty, value);
112+
}
113+
public static readonly DependencyProperty ToolTipValueProperty =
114+
DependencyProperty.Register(nameof(ToolTipValue), typeof(object), typeof(SidebarItem), new PropertyMetadata(null));
115+
116+
internal object? ChildrenValue
117+
{
118+
get => GetValue(ChildrenValueProperty);
119+
set => SetValue(ChildrenValueProperty, value);
120+
}
121+
public static readonly DependencyProperty ChildrenValueProperty =
122+
DependencyProperty.Register(nameof(ChildrenValue), typeof(object), typeof(SidebarItem), new PropertyMetadata(null));
123+
124+
private void UpdateTemplateBindings()
125+
{
126+
if (Item is not null)
127+
{
128+
TextValue = Item.Text;
129+
ToolTipValue = Item.ToolTip;
130+
ChildrenValue = Item.Children;
131+
}
132+
else
133+
{
134+
TextValue = "";
135+
ToolTipValue = null;
136+
ChildrenValue = null;
137+
}
138+
}
139+
140+
public static void OnPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
101141
{
102142
if (sender is not SidebarItem item) return;
103143
if (e.Property == DisplayModeProperty)
104144
{
105145
item.SidebarDisplayModeChanged((SidebarDisplayMode)e.OldValue);
106-
}
146+
}
107147
else if (e.Property == IsSelectedProperty)
108148
{
109149
item.UpdateSelectionState();
@@ -115,6 +155,7 @@ public static void OnPropertyChanged(DependencyObject sender, DependencyProperty
115155
else if (e.Property == ItemProperty)
116156
{
117157
item.HandleItemChange();
118-
}
119-
}
158+
item.UpdateTemplateBindings();
159+
}
160+
}
120161
}

dev/DevWinUI.Controls/Controls/Ported/Sidebar/SidebarItem.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,9 @@ public void HandleItemChange()
9595

9696
if (Item is not null)
9797
Decorator = Item.ItemDecorator;
98-
}
98+
99+
UpdateTemplateBindings();
100+
}
99101

100102
private void HookupOwners()
101103
{

dev/DevWinUI.Controls/Controls/Ported/Sidebar/SidebarStyles.xaml

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,6 @@
2828
<Setter Target="Padding" Value="0" />
2929
</Style>
3030

31-
<DataTemplate x:Key="DefaultSidebarItemTemplate">
32-
<local:SidebarItem AutomationProperties.AutomationId="{Binding Text, Mode=OneWay}"
33-
IsExpanded="{Binding IsExpanded, Mode=TwoWay}"
34-
Item="{Binding}" />
35-
</DataTemplate>
36-
3731
<Style TargetType="local:SidebarItem">
3832
<Setter Property="IsTabStop" Value="False" />
3933
<Setter Property="Template">
@@ -284,7 +278,7 @@
284278
Grid.Row="0"
285279
Height="32"
286280
Background="Transparent">
287-
<Grid x:Name="ElementGrid" Margin="0,2,0,2" HorizontalAlignment="Stretch" Background="{ThemeResource SubtleFillColorTransparentBrush}" BackgroundSizing="InnerBorderEdge" BorderThickness="1" CornerRadius="{ThemeResource ControlCornerRadius}" IsTabStop="True" TabFocusNavigation="Once" ToolTipService.ToolTip="{Binding ToolTip, Mode=OneWay}" UseSystemFocusVisuals="True">
281+
<Grid x:Name="ElementGrid" Margin="0,2,0,2" HorizontalAlignment="Stretch" Background="{ThemeResource SubtleFillColorTransparentBrush}" BackgroundSizing="InnerBorderEdge" BorderThickness="1" CornerRadius="{ThemeResource ControlCornerRadius}" IsTabStop="True" TabFocusNavigation="Once" ToolTipService.ToolTip="{Binding ToolTipValue, RelativeSource={RelativeSource Mode=TemplatedParent}}" UseSystemFocusVisuals="True">
288282
<Grid.ColumnDefinitions>
289283
<!-- Selection Indicator -->
290284
<ColumnDefinition Width="3" />
@@ -329,7 +323,7 @@
329323
AutomationProperties.AccessibilityView="Raw"
330324
FontFamily="{ThemeResource ContentControlThemeFontFamily}"
331325
LineHeight="16"
332-
Text="{Binding Text, Mode=OneWay}"
326+
Text="{Binding TextValue, RelativeSource={RelativeSource Mode=TemplatedParent}}"
333327
TextTrimming="CharacterEllipsis"
334328
TextWrapping="NoWrap" />
335329

@@ -384,16 +378,9 @@
384378
HorizontalAlignment="Stretch"
385379
local:SidebarItem.TemplateRoot="{Binding ElementName=RootPanel}"
386380
AutomationProperties.AccessibilityView="Content"
387-
ItemsSource="{Binding Children, Mode=OneWay}"
388-
XYFocusKeyboardNavigation="Enabled">
389-
<ItemsRepeater.ItemTemplate>
390-
<DataTemplate>
391-
<local:SidebarItem AutomationProperties.AutomationId="{Binding Text, Mode=OneWay}"
392-
IsInFlyout="True"
393-
Item="{Binding}" />
394-
</DataTemplate>
395-
</ItemsRepeater.ItemTemplate>
396-
</ItemsRepeater>
381+
ItemTemplate="{StaticResource DefaultSidebarItemInFlyoutTemplate}"
382+
ItemsSource="{Binding ChildrenValue, RelativeSource={RelativeSource Mode=TemplatedParent}}"
383+
XYFocusKeyboardNavigation="Enabled" />
397384
</Flyout>
398385
</FlyoutBase.AttachedFlyout>
399386
</Grid>
@@ -407,7 +394,7 @@
407394
local:SidebarItem.TemplateRoot="{Binding ElementName=RootPanel}"
408395
AutomationProperties.AccessibilityView="Content"
409396
ItemTemplate="{StaticResource DefaultSidebarItemTemplate}"
410-
ItemsSource="{Binding Children, Mode=OneWay}"
397+
ItemsSource="{Binding ChildrenValue, RelativeSource={RelativeSource Mode=TemplatedParent}}"
411398
Visibility="Collapsed"
412399
XYFocusKeyboardNavigation="Enabled">
413400
<ItemsRepeater.Layout>

dev/DevWinUI.Controls/Controls/Ported/Sidebar/SidebarView.xaml.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ public sealed partial class SidebarView : UserControl, INotifyPropertyChanged
2828

2929
public SidebarView()
3030
{
31-
InitializeComponent();
31+
Application.Current.Resources.MergedDictionaries.AddIfNotExists(new SidebarItemTemplate());
32+
InitializeComponent();
3233
}
3334

3435
internal void UpdateSelectedItemContainer(SidebarItem container)

dev/DevWinUI.Controls/Themes/Generic.xaml

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1086,9 +1086,6 @@ Themes\Styles\Controls\WaveProgressBar.xaml
10861086
<x:Double x:Key="SidebarNegativeOpenPaneLength">-300</x:Double>
10871087
<x:Double x:Key="SidebarCompactOpenPaneLength">56</x:Double>
10881088
<x:Double x:Key="SidebarNegativeCompactOpenPaneLength">-56</x:Double>
1089-
<media3D:DataTemplate x:Key="DefaultSidebarItemTemplate">
1090-
<local:SidebarItem AutomationProperties.AutomationId="{Binding Text, Mode=OneWay}" IsExpanded="{Binding IsExpanded, Mode=TwoWay}" Item="{Binding}" />
1091-
</media3D:DataTemplate>
10921089
<media3D:Style x:Key="DefaultThemedIconStyle" TargetType="local:ThemedIcon">
10931090
<media3D:Setter Property="IsTabStop" Value="False" />
10941091
<media3D:Setter Property="IconSize" Value="16" />
@@ -5328,7 +5325,7 @@ Themes\Styles\Controls\WaveProgressBar.xaml
53285325
<media3D:Rectangle x:Name="DragTargetIndicator" Grid.ColumnSpan="3" Height="2" Margin="4,-2" HorizontalAlignment="Stretch" VerticalAlignment="Top" AutomationProperties.AccessibilityView="Raw" Fill="{ThemeResource AccentFillColorDefaultBrush}" RadiusX="4" RadiusY="4" Visibility="Collapsed" />
53295326
<!-- Focusable Visual -->
53305327
<media3D:Border x:Name="ElementBorder" Grid.Row="0" Height="32" Background="Transparent">
5331-
<media3D:Grid x:Name="ElementGrid" Margin="0,2,0,2" HorizontalAlignment="Stretch" Background="{ThemeResource SubtleFillColorTransparentBrush}" BackgroundSizing="InnerBorderEdge" BorderThickness="1" CornerRadius="{ThemeResource ControlCornerRadius}" IsTabStop="True" TabFocusNavigation="Once" ToolTipService.ToolTip="{Binding ToolTip, Mode=OneWay}" UseSystemFocusVisuals="True">
5328+
<media3D:Grid x:Name="ElementGrid" Margin="0,2,0,2" HorizontalAlignment="Stretch" Background="{ThemeResource SubtleFillColorTransparentBrush}" BackgroundSizing="InnerBorderEdge" BorderThickness="1" CornerRadius="{ThemeResource ControlCornerRadius}" IsTabStop="True" TabFocusNavigation="Once" ToolTipService.ToolTip="{Binding ToolTipValue, RelativeSource={RelativeSource Mode=TemplatedParent}}" UseSystemFocusVisuals="True">
53325329
<media3D:Grid.ColumnDefinitions>
53335330
<!-- Selection Indicator -->
53345331
<media3D:ColumnDefinition Width="3" />
@@ -5344,7 +5341,7 @@ Themes\Styles\Controls\WaveProgressBar.xaml
53445341
<!-- Icon -->
53455342
<media3D:ContentPresenter x:Name="IconPresenter" Grid.Column="1" Width="16" Height="16" Margin="8,0,0,0" HorizontalAlignment="Center" VerticalAlignment="Center" AutomationProperties.AccessibilityView="Raw" Content="{Binding Icon, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}" />
53465343
<!-- Text -->
5347-
<media3D:TextBlock x:Name="ItemNameTextBlock" Grid.Column="2" Margin="12,0,12,2" HorizontalAlignment="Stretch" VerticalAlignment="Center" AutomationProperties.AccessibilityView="Raw" FontFamily="{ThemeResource ContentControlThemeFontFamily}" LineHeight="16" Text="{Binding Text, Mode=OneWay}" TextTrimming="CharacterEllipsis" TextWrapping="NoWrap" />
5344+
<media3D:TextBlock x:Name="ItemNameTextBlock" Grid.Column="2" Margin="12,0,12,2" HorizontalAlignment="Stretch" VerticalAlignment="Center" AutomationProperties.AccessibilityView="Raw" FontFamily="{ThemeResource ContentControlThemeFontFamily}" LineHeight="16" Text="{Binding TextValue, RelativeSource={RelativeSource Mode=TemplatedParent}}" TextTrimming="CharacterEllipsis" TextWrapping="NoWrap" />
53485345
<!-- Decorator -->
53495346
<media3D:ContentPresenter x:Name="ItemDecoratorPresenter" Grid.Column="3" Width="28" Margin="0" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" BorderThickness="0" Content="{Binding Decorator, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}" Visibility="Collapsed" />
53505347
<!-- Chevron -->
@@ -5367,19 +5364,13 @@ Themes\Styles\Controls\WaveProgressBar.xaml
53675364
<media3D:Setter Target="MinHeight" Value="20" />
53685365
</media3D:Style>
53695366
</media3D:Flyout.FlyoutPresenterStyle>
5370-
<media3D:ItemsRepeater x:Name="FlyoutChildrenPresenter" HorizontalAlignment="Stretch" local:SidebarItem.TemplateRoot="{Binding ElementName=RootPanel}" AutomationProperties.AccessibilityView="Content" ItemsSource="{Binding Children, Mode=OneWay}" XYFocusKeyboardNavigation="Enabled">
5371-
<media3D:ItemsRepeater.ItemTemplate>
5372-
<media3D:DataTemplate>
5373-
<local:SidebarItem AutomationProperties.AutomationId="{Binding Text, Mode=OneWay}" IsInFlyout="True" Item="{Binding}" />
5374-
</media3D:DataTemplate>
5375-
</media3D:ItemsRepeater.ItemTemplate>
5376-
</media3D:ItemsRepeater>
5367+
<media3D:ItemsRepeater x:Name="FlyoutChildrenPresenter" HorizontalAlignment="Stretch" local:SidebarItem.TemplateRoot="{Binding ElementName=RootPanel}" AutomationProperties.AccessibilityView="Content" ItemTemplate="{StaticResource DefaultSidebarItemInFlyoutTemplate}" ItemsSource="{Binding ChildrenValue, RelativeSource={RelativeSource Mode=TemplatedParent}}" XYFocusKeyboardNavigation="Enabled" />
53775368
</media3D:Flyout>
53785369
</media3D:FlyoutBase.AttachedFlyout>
53795370
</media3D:Grid>
53805371
</media3D:Border>
53815372
<!-- Children -->
5382-
<media3D:ItemsRepeater x:Name="ChildrenPresenter" Grid.Row="2" Margin="-4,2" HorizontalAlignment="Stretch" local:SidebarItem.TemplateRoot="{Binding ElementName=RootPanel}" AutomationProperties.AccessibilityView="Content" ItemTemplate="{StaticResource DefaultSidebarItemTemplate}" ItemsSource="{Binding Children, Mode=OneWay}" Visibility="Collapsed" XYFocusKeyboardNavigation="Enabled">
5373+
<media3D:ItemsRepeater x:Name="ChildrenPresenter" Grid.Row="2" Margin="-4,2" HorizontalAlignment="Stretch" local:SidebarItem.TemplateRoot="{Binding ElementName=RootPanel}" AutomationProperties.AccessibilityView="Content" ItemTemplate="{StaticResource DefaultSidebarItemTemplate}" ItemsSource="{Binding ChildrenValue, RelativeSource={RelativeSource Mode=TemplatedParent}}" Visibility="Collapsed" XYFocusKeyboardNavigation="Enabled">
53835374
<media3D:ItemsRepeater.Layout>
53845375
<media3D:StackLayout />
53855376
</media3D:ItemsRepeater.Layout>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<ResourceDictionary x:Class="DevWinUI.SidebarItemTemplate"
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
xmlns:local="using:DevWinUI">
5+
6+
<DataTemplate x:Key="DefaultSidebarItemTemplate"
7+
x:DataType="local:ISidebarItemModel">
8+
<local:SidebarItem AutomationProperties.AutomationId="{x:Bind Text, Mode=OneWay}"
9+
IsExpanded="{x:Bind IsExpanded, Mode=TwoWay}"
10+
Item="{x:Bind}" />
11+
</DataTemplate>
12+
13+
<DataTemplate x:Key="DefaultSidebarItemInFlyoutTemplate"
14+
x:DataType="local:ISidebarItemModel">
15+
<local:SidebarItem AutomationProperties.AutomationId="{x:Bind Text, Mode=OneWay}"
16+
IsInFlyout="True"
17+
Item="{x:Bind}" />
18+
</DataTemplate>
19+
</ResourceDictionary>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace DevWinUI;
2+
public sealed partial class SidebarItemTemplate : ResourceDictionary
3+
{
4+
public SidebarItemTemplate()
5+
{
6+
this.InitializeComponent();
7+
}
8+
}

dev/DevWinUI.Gallery/Models/FolderItem.cs

Lines changed: 11 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,16 @@
22

33
namespace DevWinUIGallery.Models;
44

5-
public sealed partial class FolderItem : ISidebarItemModel, INotifyPropertyChanged
5+
public sealed partial class FolderItem : ObservableObject, ISidebarItemModel
66
{
7-
private string text = "";
8-
public string Text
9-
{
10-
get => text;
11-
set => SetProperty(ref text, value, nameof(Text));
12-
}
7+
[ObservableProperty]
8+
public partial string FolderText { get; set; } = "";
139

14-
private string path = "";
15-
public string Path
16-
{
17-
get => path;
18-
set => SetProperty(ref path, value, nameof(Path));
19-
}
10+
[ObservableProperty]
11+
public partial string Path { get; set; } = "";
2012

21-
private ImageIconSource icon = new ImageIconSource();
22-
public ImageIconSource Icon
23-
{
24-
get => icon;
25-
set
26-
{
27-
SetProperty(ref icon, value, nameof(Icon));
28-
OnPropertyChanged(nameof(IconSource));
29-
}
30-
}
13+
[ObservableProperty]
14+
public partial ImageIconSource Icon { get; set; } = new ImageIconSource();
3115

3216
public object? Children { get; set; } = null;
3317

@@ -39,16 +23,11 @@ public ImageIconSource Icon
3923

4024
public bool PaddedItem => false;
4125

42-
public event PropertyChangedEventHandler? PropertyChanged;
43-
44-
private void OnPropertyChanged(string propertyName)
45-
=> PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
26+
public string Text => FolderText;
4627

47-
private bool SetProperty<T>(ref T storage, T value, string propertyName)
28+
partial void OnIconChanged(ImageIconSource value)
4829
{
49-
if (Equals(storage, value)) return false;
50-
storage = value;
51-
OnPropertyChanged(propertyName);
52-
return true;
30+
// Notify that IconSource also changed when Icon changes
31+
OnPropertyChanged(nameof(IconSource));
5332
}
5433
}

dev/DevWinUI.Gallery/ViewModels/BaseViewModel.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public partial class BaseViewModel : ObservableObject
1010
{
1111
new FolderItem()
1212
{
13-
Text = "Home",
13+
FolderText = "Home",
1414
Path = "C:\\Fonts",
1515
Icon = new ImageIconSource()
1616
{
@@ -19,7 +19,7 @@ public partial class BaseViewModel : ObservableObject
1919
},
2020
new FolderItem()
2121
{
22-
Text = "Folder",
22+
FolderText = "Folder",
2323
Path = "C:\\Folder",
2424
IsExpanded = true,
2525
Icon = new ImageIconSource()
@@ -30,7 +30,7 @@ public partial class BaseViewModel : ObservableObject
3030
{
3131
new FolderItem()
3232
{
33-
Text = "SubFolder1",
33+
FolderText = "SubFolder1",
3434
Path = "C:\\Folder\\SubFolder1",
3535
Icon = new ImageIconSource()
3636
{
@@ -39,7 +39,7 @@ public partial class BaseViewModel : ObservableObject
3939
},
4040
new FolderItem()
4141
{
42-
Text = "SubFolder2",
42+
FolderText = "SubFolder2",
4343
Path = "C:\\Folder\\SubFolder2",
4444
Icon = new ImageIconSource()
4545
{
@@ -50,7 +50,7 @@ public partial class BaseViewModel : ObservableObject
5050
},
5151
new FolderItem()
5252
{
53-
Text = "Drive",
53+
FolderText = "Drive",
5454
Path = "D:\\Drive",
5555
Icon = new ImageIconSource()
5656
{

dev/DevWinUI.Gallery/Views/Pages/Features/SideBarViewPage.xaml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,6 @@
1010

1111
<ScrollViewer>
1212
<StackPanel Margin="10" dev:PanelAttach.ChildrenTransitions="Default" Spacing="10">
13-
<InfoBar Title="AOT Not Supported"
14-
IsClosable="False"
15-
IsOpen="True"
16-
Message="This sample does not work with AOT."
17-
Severity="Warning" />
1813
<local:ControlExample CSharpSource="Features/SideBarViewPage_cs.txt"
1914
DocPage="controls/sidebarView"
2015
HeaderText="SidebarView">

0 commit comments

Comments
 (0)