Browse Source

Use ContentControl in DropDown template.

Instead of `ContentPresenter`. This fixes a memory leak whereby
`DropDown` wasn't correctly parenting the `Rectangle` created for
`SelectionBoxItem` when the selected item is a control. Fixes #706.
pull/712/head
Steven Kirk 10 years ago
parent
commit
a0e880581e
  1. 8
      src/Avalonia.Themes.Default/DropDown.xaml
  2. 63
      tests/Avalonia.Controls.UnitTests/DropDownTests.cs

8
src/Avalonia.Themes.Default/DropDown.xaml

@ -10,10 +10,10 @@
BorderBrush="{TemplateBinding BorderBrush}" BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"> BorderThickness="{TemplateBinding BorderThickness}">
<Grid ColumnDefinitions="*,Auto"> <Grid ColumnDefinitions="*,Auto">
<ContentPresenter Content="{TemplateBinding SelectionBoxItem}" <ContentControl Content="{TemplateBinding SelectionBoxItem}"
Margin="{TemplateBinding Padding}" Margin="{TemplateBinding Padding}"
HorizontalAlignment="Left" HorizontalAlignment="Left"
VerticalAlignment="Center"/> VerticalAlignment="Center"/>
<ToggleButton Name="toggle" <ToggleButton Name="toggle"
BorderThickness="0" BorderThickness="0"
Background="Transparent" Background="Transparent"

63
tests/Avalonia.Controls.UnitTests/DropDownTests.cs

@ -1,20 +1,65 @@
// Copyright (c) The Avalonia Project. All rights reserved. // Copyright (c) The Avalonia Project. All rights reserved.
// Licensed under the MIT license. See licence.md file in the project root for full license information. // Licensed under the MIT license. See licence.md file in the project root for full license information.
using System;
using System.Linq; using System.Linq;
using Avalonia.Controls.Presenters; using Avalonia.Controls.Presenters;
using Avalonia.Controls.Primitives; using Avalonia.Controls.Primitives;
using Avalonia.Controls.Shapes;
using Avalonia.Controls.Templates; using Avalonia.Controls.Templates;
using Avalonia.Platform; using Avalonia.LogicalTree;
using Ploeh.AutoFixture; using Avalonia.Media;
using Ploeh.AutoFixture.AutoMoq; using Avalonia.UnitTests;
using Avalonia.VisualTree;
using Xunit; using Xunit;
namespace Avalonia.Controls.UnitTests namespace Avalonia.Controls.UnitTests
{ {
public class DropDownTests public class DropDownTests
{ {
[Fact]
public void SelectionBoxItem_Is_Rectangle_With_VisualBrush_When_Selection_Is_Control()
{
var items = new[] { new Canvas() };
var target = new DropDown
{
Items = items,
SelectedIndex = 0,
};
var rectangle = target.GetValue(DropDown.SelectionBoxItemProperty) as Rectangle;
Assert.NotNull(rectangle);
var brush = rectangle.Fill as VisualBrush;
Assert.NotNull(brush);
Assert.Same(items[0], brush.Visual);
}
[Fact]
public void SelectionBoxItem_Rectangle_Is_Removed_From_Logical_Tree()
{
var target = new DropDown
{
Items = new[] { new Canvas() },
SelectedIndex = 0,
Template = GetTemplate(),
};
var root = new TestRoot { Child = target };
target.ApplyTemplate();
target.Presenter.ApplyTemplate();
var rectangle = target.GetValue(DropDown.SelectionBoxItemProperty) as Rectangle;
Assert.True(((ILogical)target).IsAttachedToLogicalTree);
Assert.True(((ILogical)rectangle).IsAttachedToLogicalTree);
rectangle.DetachedFromLogicalTree += (s, e) => { };
root.Child = null;
Assert.False(((ILogical)target).IsAttachedToLogicalTree);
Assert.False(((ILogical)rectangle).IsAttachedToLogicalTree);
}
private FuncControlTemplate GetTemplate() private FuncControlTemplate GetTemplate()
{ {
return new FuncControlTemplate<DropDown>(parent => return new FuncControlTemplate<DropDown>(parent =>
@ -26,8 +71,7 @@ namespace Avalonia.Controls.UnitTests
{ {
new ContentControl new ContentControl
{ {
Name = "contentControl", [!ContentControl.ContentProperty] = parent[!DropDown.SelectionBoxItemProperty],
[~ContentPresenter.ContentProperty] = parent[~DropDown.SelectionBoxItemProperty],
}, },
new ToggleButton new ToggleButton
{ {
@ -35,7 +79,12 @@ namespace Avalonia.Controls.UnitTests
}, },
new Popup new Popup
{ {
Name = "popup", Name = "PART_Popup",
Child = new ItemsPresenter
{
Name = "PART_ItemsPresenter",
[!ItemsPresenter.ItemsProperty] = parent[!DropDown.ItemsProperty],
}
} }
} }
}; };

Loading…
Cancel
Save