-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ControlTemplateScope and enable it on ItemsPanelTemplate (#17483)
- Loading branch information
Showing
6 changed files
with
172 additions
and
3 deletions.
There are no files selected for viewing
13 changes: 13 additions & 0 deletions
13
src/Avalonia.Base/Metadata/ControlTemplateScopeAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using System; | ||
|
||
namespace Avalonia.Metadata; | ||
|
||
/// <summary> | ||
/// Indicates that a type acts as a control template scope (for example, TemplateBindings are expected to work). | ||
/// Types annotated with this attribute may provide a TargetType property. | ||
/// </summary> | ||
[AttributeUsage( | ||
AttributeTargets.Class | AttributeTargets.Interface | AttributeTargets.Struct, | ||
AllowMultiple = false, | ||
Inherited = true)] | ||
public sealed class ControlTemplateScopeAttribute : Attribute; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
tests/Avalonia.Markup.Xaml.UnitTests/Xaml/ItemsPanelTemplateTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
using Avalonia.Controls; | ||
using Avalonia.Controls.Presenters; | ||
using Avalonia.Media; | ||
using Avalonia.UnitTests; | ||
using Avalonia.VisualTree; | ||
using Xunit; | ||
|
||
namespace Avalonia.Markup.Xaml.UnitTests.Xaml; | ||
|
||
public class ItemsPanelTemplateTests | ||
{ | ||
[Fact] | ||
public void ItemsPanelTemplate_In_Style_Allows_TemplateBinding() | ||
{ | ||
using (UnitTestApplication.Start(TestServices.StyledWindow)) | ||
{ | ||
var window = (Window)AvaloniaRuntimeXamlLoader.Load( | ||
""" | ||
<Window xmlns="https://github.com/avaloniaui" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> | ||
<Window.Styles> | ||
<Style Selector="ListBox"> | ||
<Setter Property="Template"> | ||
<ControlTemplate> | ||
<ItemsPresenter Name="PART_ItemsPresenter" | ||
ItemsPanel="{TemplateBinding ItemsPanel}" /> | ||
</ControlTemplate> | ||
</Setter> | ||
<Setter Property="ItemsPanel"> | ||
<ItemsPanelTemplate> | ||
<Panel Background="{TemplateBinding Background}" | ||
Tag="{TemplateBinding ItemsSource}" /> | ||
</ItemsPanelTemplate> | ||
</Setter> | ||
</Style> | ||
</Window.Styles> | ||
<ListBox Background="DodgerBlue" /> | ||
</Window> | ||
"""); | ||
var listBox = Assert.IsType<ListBox>(window.Content); | ||
var items = new[] { "foo", "bar" }; | ||
listBox.ItemsSource = items; | ||
|
||
window.ApplyTemplate(); | ||
listBox.ApplyTemplate(); | ||
|
||
var itemsPresenter = listBox.FindDescendantOfType<ItemsPresenter>(); | ||
Assert.NotNull(itemsPresenter); | ||
itemsPresenter.ApplyTemplate(); | ||
|
||
var panel = itemsPresenter.Panel; | ||
Assert.NotNull(panel); | ||
Assert.Equal(Brushes.DodgerBlue, panel.Background); | ||
Assert.Same(items, panel.Tag); | ||
} | ||
} | ||
|
||
[Fact] | ||
public void ItemsPanelTemplate_In_Control_Allows_TemplateBinding() | ||
{ | ||
using (UnitTestApplication.Start(TestServices.StyledWindow)) | ||
{ | ||
var window = (Window)AvaloniaRuntimeXamlLoader.Load( | ||
""" | ||
<Window xmlns="https://github.com/avaloniaui" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> | ||
<ListBox Background="DodgerBlue"> | ||
<ListBox.Template> | ||
<ControlTemplate> | ||
<ItemsPresenter Name="PART_ItemsPresenter" | ||
ItemsPanel="{TemplateBinding ItemsPanel}" /> | ||
</ControlTemplate> | ||
</ListBox.Template> | ||
<ListBox.ItemsPanel> | ||
<ItemsPanelTemplate> | ||
<Panel Background="{TemplateBinding Background}" | ||
Tag="{TemplateBinding ItemsSource}" /> | ||
</ItemsPanelTemplate> | ||
</ListBox.ItemsPanel> | ||
</ListBox> | ||
</Window> | ||
"""); | ||
var listBox = Assert.IsType<ListBox>(window.Content); | ||
var items = new[] { "foo", "bar" }; | ||
listBox.ItemsSource = items; | ||
|
||
window.ApplyTemplate(); | ||
listBox.ApplyTemplate(); | ||
|
||
var itemsPresenter = listBox.FindDescendantOfType<ItemsPresenter>(); | ||
Assert.NotNull(itemsPresenter); | ||
itemsPresenter.ApplyTemplate(); | ||
|
||
var panel = itemsPresenter.Panel; | ||
Assert.NotNull(panel); | ||
Assert.Equal(Brushes.DodgerBlue, panel.Background); | ||
Assert.Same(items, panel.Tag); | ||
} | ||
} | ||
} |