diff --git a/samples/ControlCatalog/Models/Person.cs b/samples/ControlCatalog/Models/Person.cs
index 47f41bc584..cd70fa3959 100644
--- a/samples/ControlCatalog/Models/Person.cs
+++ b/samples/ControlCatalog/Models/Person.cs
@@ -16,6 +16,7 @@ namespace ControlCatalog.Models
string _firstName;
string _lastName;
bool _isBanned;
+ private int _age;
public string FirstName
{
@@ -59,6 +60,20 @@ namespace ControlCatalog.Models
}
}
+
+ ///
+ /// Gets or sets the age of the person
+ ///
+ public int Age
+ {
+ get => _age;
+ set
+ {
+ _age = value;
+ OnPropertyChanged(nameof(Age));
+ }
+ }
+
Dictionary> _errorLookup = new Dictionary>();
void SetError(string propertyName, string error)
diff --git a/samples/ControlCatalog/Pages/DataGridPage.xaml b/samples/ControlCatalog/Pages/DataGridPage.xaml
index 63e873d9b5..f7e3cf2441 100644
--- a/samples/ControlCatalog/Pages/DataGridPage.xaml
+++ b/samples/ControlCatalog/Pages/DataGridPage.xaml
@@ -64,6 +64,18 @@
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/samples/ControlCatalog/Pages/DataGridPage.xaml.cs b/samples/ControlCatalog/Pages/DataGridPage.xaml.cs
index dc5cc49a90..c010d38eac 100644
--- a/samples/ControlCatalog/Pages/DataGridPage.xaml.cs
+++ b/samples/ControlCatalog/Pages/DataGridPage.xaml.cs
@@ -6,7 +6,9 @@ using Avalonia.Controls;
using Avalonia.Markup.Xaml;
using ControlCatalog.Models;
using Avalonia.Collections;
+using Avalonia.Controls.Primitives;
using Avalonia.Data;
+using Avalonia.Threading;
namespace ControlCatalog.Pages
{
@@ -48,9 +50,9 @@ namespace ControlCatalog.Pages
var items = new List
{
- new Person { FirstName = "John", LastName = "Doe" },
- new Person { FirstName = "Elizabeth", LastName = "Thomas", IsBanned = true },
- new Person { FirstName = "Zack", LastName = "Ward" }
+ new Person { FirstName = "John", LastName = "Doe" , Age = 30},
+ new Person { FirstName = "Elizabeth", LastName = "Thomas", IsBanned = true , Age = 40 },
+ new Person { FirstName = "Zack", LastName = "Ward" , Age = 50 }
};
var collectionView3 = new DataGridCollectionView(items);
@@ -84,5 +86,19 @@ namespace ControlCatalog.Pages
return Comparer.Default.Compare(x, y);
}
}
+
+ private void NumericUpDown_OnTemplateApplied(object sender, TemplateAppliedEventArgs e)
+ {
+ // We want to focus the TextBox of the NumericUpDown. To do so we search for this control when the template
+ // is applied, but we postpone the action until the control is actually loaded.
+ if (e.NameScope.Find("PART_TextBox") is {} textBox)
+ {
+ Dispatcher.UIThread.InvokeAsync(() =>
+ {
+ textBox.Focus();
+ textBox.SelectAll();
+ }, DispatcherPriority.Loaded);
+ }
+ }
}
}
diff --git a/src/Avalonia.Controls.DataGrid/DataGridColumn.cs b/src/Avalonia.Controls.DataGrid/DataGridColumn.cs
index 6b515503aa..8501ce3896 100644
--- a/src/Avalonia.Controls.DataGrid/DataGridColumn.cs
+++ b/src/Avalonia.Controls.DataGrid/DataGridColumn.cs
@@ -448,7 +448,7 @@ namespace Avalonia.Controls
internal set;
}
- public bool IsReadOnly
+ public virtual bool IsReadOnly
{
get
{
diff --git a/src/Avalonia.Controls.DataGrid/DataGridTemplateColumn.cs b/src/Avalonia.Controls.DataGrid/DataGridTemplateColumn.cs
index 7e95dd100c..0e754d5815 100644
--- a/src/Avalonia.Controls.DataGrid/DataGridTemplateColumn.cs
+++ b/src/Avalonia.Controls.DataGrid/DataGridTemplateColumn.cs
@@ -1,4 +1,4 @@
-// (c) Copyright Microsoft Corporation.
+// (c) Copyright Microsoft Corporation.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.
@@ -15,7 +15,7 @@ namespace Avalonia.Controls
{
public class DataGridTemplateColumn : DataGridColumn
{
- IDataTemplate _cellTemplate;
+ private IDataTemplate _cellTemplate;
public static readonly DirectProperty CellTemplateProperty =
AvaloniaProperty.RegisterDirect(
@@ -30,17 +30,38 @@ namespace Avalonia.Controls
set { SetAndRaise(CellTemplateProperty, ref _cellTemplate, value); }
}
+ private IDataTemplate _cellEditingCellTemplate;
+
+ ///
+ /// Defines the property.
+ ///
+ public static readonly DirectProperty CellEditingTemplateProperty =
+ AvaloniaProperty.RegisterDirect(
+ nameof(CellEditingTemplate),
+ o => o.CellEditingTemplate,
+ (o, v) => o.CellEditingTemplate = v);
+
+ ///
+ /// Gets or sets the which is used for the editing mode of the current
+ ///
+ ///
+ /// An for the editing mode of the current
+ ///
+ ///
+ /// If this property is the column is read-only.
+ ///
+ public IDataTemplate CellEditingTemplate
+ {
+ get => _cellEditingCellTemplate;
+ set => SetAndRaise(CellEditingTemplateProperty, ref _cellEditingCellTemplate, value);
+ }
+
private void OnCellTemplateChanged(AvaloniaPropertyChangedEventArgs e)
{
var oldValue = (IDataTemplate)e.OldValue;
var value = (IDataTemplate)e.NewValue;
}
- public DataGridTemplateColumn()
- {
- IsReadOnly = true;
- }
-
protected override IControl GenerateElement(DataGridCell cell, object dataItem)
{
if(CellTemplate != null)
@@ -60,7 +81,22 @@ namespace Avalonia.Controls
protected override IControl GenerateEditingElement(DataGridCell cell, object dataItem, out ICellEditBinding binding)
{
binding = null;
- return GenerateElement(cell, dataItem);
+ if(CellEditingTemplate != null)
+ {
+ return CellEditingTemplate.Build(dataItem);
+ }
+ else if (CellTemplate != null)
+ {
+ return CellTemplate.Build(dataItem);
+ }
+ if (Design.IsDesignMode)
+ {
+ return null;
+ }
+ else
+ {
+ throw DataGridError.DataGridTemplateColumn.MissingTemplateForType(typeof(DataGridTemplateColumn));
+ }
}
protected override object PrepareCellForEdit(IControl editingElement, RoutedEventArgs editingEventArgs)
@@ -70,12 +106,30 @@ namespace Avalonia.Controls
protected internal override void RefreshCellContent(IControl element, string propertyName)
{
- if(propertyName == nameof(CellTemplate) && element.Parent is DataGridCell cell)
+ var cell = element.Parent as DataGridCell;
+ if(propertyName == nameof(CellTemplate) && cell is not null)
{
cell.Content = GenerateElement(cell, cell.DataContext);
}
base.RefreshCellContent(element, propertyName);
}
+
+ public override bool IsReadOnly
+ {
+ get
+ {
+ if (CellEditingTemplate is null)
+ {
+ return true;
+ }
+
+ return base.IsReadOnly;
+ }
+ set
+ {
+ base.IsReadOnly = value;
+ }
+ }
}
}