diff --git a/src/Avalonia.Controls/ColumnDefinitions.cs b/src/Avalonia.Controls/ColumnDefinitions.cs
index ae7756e7d1..7fb763c6fd 100644
--- a/src/Avalonia.Controls/ColumnDefinitions.cs
+++ b/src/Avalonia.Controls/ColumnDefinitions.cs
@@ -11,7 +11,7 @@ namespace Avalonia.Controls
///
/// A collection of s.
///
- public class ColumnDefinitions : AvaloniaList
+ public class ColumnDefinitions : DefinitionList
{
///
/// Initializes a new instance of the class.
@@ -23,31 +23,6 @@ namespace Avalonia.Controls
this.TrackItemPropertyChanged(delegate { IsDirty = true; });
}
- internal bool IsDirty { get; set; } = true;
- internal Grid Parent { get; set; }
-
- private void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
- {
- foreach (var nI in this.Select((d, i) => (d, i)))
- nI.d._parentIndex = nI.i;
-
- foreach (var nD in e.NewItems?.Cast()
- ?? Enumerable.Empty())
- {
- nD.Parent = this.Parent;
- nD.OnEnterParentTree();
- }
-
- foreach (var oD in e.OldItems?.Cast()
- ?? Enumerable.Empty())
- {
- oD.Parent = null;
- oD.OnExitParentTree();
- }
-
- IsDirty = true;
- }
-
///
/// Initializes a new instance of the class.
///
diff --git a/src/Avalonia.Controls/DefinitionBase.cs b/src/Avalonia.Controls/DefinitionBase.cs
index 36dcb714c4..8899c38bf9 100644
--- a/src/Avalonia.Controls/DefinitionBase.cs
+++ b/src/Avalonia.Controls/DefinitionBase.cs
@@ -315,7 +315,7 @@ namespace Avalonia.Controls
}
set
{
- Debug.Assert(value >= -1 && _parentIndex != value);
+ Debug.Assert(value >= -1);
_parentIndex = value;
}
}
diff --git a/src/Avalonia.Controls/DefinitionList.cs b/src/Avalonia.Controls/DefinitionList.cs
new file mode 100644
index 0000000000..97c8b7f7ec
--- /dev/null
+++ b/src/Avalonia.Controls/DefinitionList.cs
@@ -0,0 +1,56 @@
+// 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.
+
+using System;
+using System.Collections.Specialized;
+using System.Linq;
+using Avalonia.Collections;
+
+namespace Avalonia.Controls
+{
+ public abstract class DefinitionList : AvaloniaList where T : DefinitionBase
+ {
+ internal bool IsDirty = true;
+ private Grid _parent;
+
+ internal Grid Parent
+ {
+ get => _parent;
+ set => SetParent(value);
+ }
+
+
+ private void SetParent(Grid value)
+ {
+ _parent = value;
+
+ foreach (var pair in this.Select((definitions, index) => (definitions, index)))
+ {
+ pair.definitions.Parent = value;
+ pair.definitions.Index = pair.index;
+ }
+ }
+
+ internal void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
+ {
+ foreach (var nI in this.Select((d, i) => (d, i)))
+ nI.d._parentIndex = nI.i;
+
+ foreach (var nD in e.NewItems?.Cast()
+ ?? Enumerable.Empty())
+ {
+ nD.Parent = this.Parent;
+ nD.OnEnterParentTree();
+ }
+
+ foreach (var oD in e.OldItems?.Cast()
+ ?? Enumerable.Empty())
+ {
+ oD.Parent = null;
+ oD.OnExitParentTree();
+ }
+
+ IsDirty = true;
+ }
+ }
+}
\ No newline at end of file
diff --git a/src/Avalonia.Controls/Grid.cs b/src/Avalonia.Controls/Grid.cs
index 240ebd4091..fc61c409f0 100644
--- a/src/Avalonia.Controls/Grid.cs
+++ b/src/Avalonia.Controls/Grid.cs
@@ -933,7 +933,7 @@ namespace Avalonia.Controls
{
if (extData.DefinitionsU == null)
{
- extData.DefinitionsU = new DefinitionBase[] { new ColumnDefinition() };
+ extData.DefinitionsU = new DefinitionBase[] { new ColumnDefinition() { Parent = this } };
}
}
else
@@ -942,7 +942,7 @@ namespace Avalonia.Controls
{
// if column definitions collection is empty
// mockup array with one column
- extData.DefinitionsU = new DefinitionBase[] { new ColumnDefinition() };
+ extData.DefinitionsU = new DefinitionBase[] { new ColumnDefinition() { Parent = this } };
}
else
{
@@ -974,7 +974,7 @@ namespace Avalonia.Controls
{
if (extData.DefinitionsV == null)
{
- extData.DefinitionsV = new DefinitionBase[] { new RowDefinition() };
+ extData.DefinitionsV = new DefinitionBase[] { new RowDefinition() { Parent = this } };
}
}
else
@@ -983,7 +983,7 @@ namespace Avalonia.Controls
{
// if row definitions collection is empty
// mockup array with one row
- extData.DefinitionsV = new DefinitionBase[] { new RowDefinition() };
+ extData.DefinitionsV = new DefinitionBase[] { new RowDefinition() { Parent = this } };
}
else
{
diff --git a/src/Avalonia.Controls/RowDefinitions.cs b/src/Avalonia.Controls/RowDefinitions.cs
index c12d284977..c5c8c75173 100644
--- a/src/Avalonia.Controls/RowDefinitions.cs
+++ b/src/Avalonia.Controls/RowDefinitions.cs
@@ -1,7 +1,6 @@
// 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.
-using System.Collections.Specialized;
using System.Linq;
using Avalonia.Collections;
@@ -10,7 +9,7 @@ namespace Avalonia.Controls
///
/// A collection of s.
///
- public class RowDefinitions : AvaloniaList
+ public class RowDefinitions : DefinitionList
{
///
/// Initializes a new instance of the class.
@@ -22,31 +21,6 @@ namespace Avalonia.Controls
this.TrackItemPropertyChanged(delegate { IsDirty = true; });
}
- internal bool IsDirty { get; set; } = true;
- internal Grid Parent { get; set; }
-
- private void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
- {
- foreach (var nI in this.Select((d, i) => (d, i)))
- nI.d._parentIndex = nI.i;
-
- foreach (var nD in e.NewItems?.Cast()
- ?? Enumerable.Empty())
- {
- nD.Parent = this.Parent;
- nD.OnEnterParentTree();
- }
-
- foreach (var oD in e.OldItems?.Cast()
- ?? Enumerable.Empty())
- {
- oD.Parent = null;
- oD.OnExitParentTree();
- }
-
- IsDirty = true;
- }
-
///
/// Initializes a new instance of the class.
///