diff --git a/src/AtomUI.Controls.DataGrid/DataGrid.Privates.cs b/src/AtomUI.Controls.DataGrid/DataGrid.Privates.cs index 44a3e5187..65ff52d9e 100644 --- a/src/AtomUI.Controls.DataGrid/DataGrid.Privates.cs +++ b/src/AtomUI.Controls.DataGrid/DataGrid.Privates.cs @@ -52,6 +52,12 @@ public partial class DataGrid o => o.HeaderCornerRadius, (o, v) => o.HeaderCornerRadius = v); + internal static readonly DirectProperty IsEmptyDataSourceProperty = + AvaloniaProperty.RegisterDirect( + nameof(IsEmptyDataSource), + o => o.IsEmptyDataSource, + (o, v) => o.IsEmptyDataSource = v); + internal bool IsGroupHeaderMode { get => _isGroupHeaderMode; @@ -73,6 +79,13 @@ public partial class DataGrid } private CornerRadius _headerCornerRadius; + internal bool IsEmptyDataSource + { + get => _isEmptyDataSource; + set => SetAndRaise(IsEmptyDataSourceProperty, ref _isEmptyDataSource, value); + } + private bool _isEmptyDataSource = false; + internal IndexToValueTable RowGroupHeadersTable { get; private set; } internal DataGridDisplayData DisplayData { get; private set; } @@ -1306,13 +1319,25 @@ public partial class DataGrid : default; } - Debug.Assert(newCollectionView != null); - - if (newCollectionView.Filter == null) + if (oldCollectionView != null) { - // TODO 不知道这样循环会不会有内存泄露的风险 - _defaultFilter = new DataGridDefaultFilter(newCollectionView); - newCollectionView.Filter = _defaultFilter; + oldCollectionView.CollectionChanged -= HandleDataCollectionViewChanged; + } + + if (newCollectionView != null) + { + newCollectionView.CollectionChanged += HandleDataCollectionViewChanged; + IsEmptyDataSource = newCollectionView.IsEmpty; + if (newCollectionView.Filter == null) + { + // TODO 不知道这样循环会不会有内存泄露的风险 + _defaultFilter = new DataGridDefaultFilter(newCollectionView); + newCollectionView.Filter = _defaultFilter; + } + } + else + { + IsEmptyDataSource = true; } DataConnection.DataSource = newCollectionView; @@ -1363,6 +1388,14 @@ public partial class DataGrid } } + private void HandleDataCollectionViewChanged(object? sender, NotifyCollectionChangedEventArgs args) + { + if (sender is DataGridCollectionView view) + { + IsEmptyDataSource = view.IsEmpty; + } + } + internal void UpdatePseudoClasses() { var visibleColumns = ColumnsInternal.GetVisibleColumns().ToList(); diff --git a/src/AtomUI.Controls.DataGrid/DataGrid.cs b/src/AtomUI.Controls.DataGrid/DataGrid.cs index 7398fccc9..ab5079638 100644 --- a/src/AtomUI.Controls.DataGrid/DataGrid.cs +++ b/src/AtomUI.Controls.DataGrid/DataGrid.cs @@ -227,6 +227,12 @@ public partial class DataGrid : TemplatedControl, public static readonly StyledProperty FooterTemplateProperty = AvaloniaProperty.Register(nameof(FooterTemplate)); + + public static readonly StyledProperty EmptyIndicatorProperty = + AvaloniaProperty.Register(nameof(EmptyIndicator)); + + public static readonly StyledProperty EmptyIndicatorTemplateProperty = + AvaloniaProperty.Register(nameof(EmptyIndicatorTemplate)); public static readonly StyledProperty IsMotionEnabledProperty = MotionAwareControlProperty.IsMotionEnabledProperty.AddOwner(); @@ -586,11 +592,24 @@ public partial class DataGrid : TemplatedControl, get => GetValue(FooterTemplateProperty); set => SetValue(FooterTemplateProperty, value); } + + [DependsOn(nameof(EmptyIndicatorTemplate))] + public object? EmptyIndicator + { + get => GetValue(EmptyIndicatorProperty); + set => SetValue(EmptyIndicatorProperty, value); + } + + public IDataTemplate? EmptyIndicatorTemplate + { + get => GetValue(EmptyIndicatorTemplateProperty); + set => SetValue(EmptyIndicatorTemplateProperty, value); + } /// - /// Gets or sets the column that contains the current cell. - /// - public DataGridColumn? CurrentColumn + /// Gets or sets the column that contains the current cell. + /// + public DataGridColumn? CurrentColumn { get { @@ -1530,6 +1549,11 @@ public partial class DataGrid : TemplatedControl, } ConfigureHeaderCornerRadius(); + SetValue(EmptyIndicatorProperty, new EmptyIndicator() + { + SizeType = SizeType.Middle, + PresetImage = PresetEmptyImage.Simple + }, BindingPriority.Template); } /// diff --git a/src/AtomUI.Controls.DataGrid/Row/DataGridEmptyDataIndicator.cs b/src/AtomUI.Controls.DataGrid/Row/DataGridEmptyDataIndicator.cs deleted file mode 100644 index 35be4d09a..000000000 --- a/src/AtomUI.Controls.DataGrid/Row/DataGridEmptyDataIndicator.cs +++ /dev/null @@ -1,8 +0,0 @@ -using Avalonia.Controls.Primitives; - -namespace AtomUI.Controls; - -internal class DataGridEmptyDataIndicator : TemplatedControl -{ - -} \ No newline at end of file diff --git a/src/AtomUI.Controls.DataGrid/Themes/DataGridEmptyDataIndicator.axaml b/src/AtomUI.Controls.DataGrid/Themes/DataGridEmptyDataIndicator.axaml deleted file mode 100644 index b65e7b8b5..000000000 --- a/src/AtomUI.Controls.DataGrid/Themes/DataGridEmptyDataIndicator.axaml +++ /dev/null @@ -1,7 +0,0 @@ - - - - \ No newline at end of file diff --git a/src/AtomUI.Controls.DataGrid/Themes/DataGridTheme.axaml b/src/AtomUI.Controls.DataGrid/Themes/DataGridTheme.axaml index d6bddd860..7c32fa09d 100644 --- a/src/AtomUI.Controls.DataGrid/Themes/DataGridTheme.axaml +++ b/src/AtomUI.Controls.DataGrid/Themes/DataGridTheme.axaml @@ -67,7 +67,8 @@ Grid.Column="0" Grid.RowSpan="2" Grid.ColumnSpan="3" - ScrollViewer.IsScrollInertiaEnabled="{TemplateBinding IsScrollInertiaEnabled}"> + ScrollViewer.IsScrollInertiaEnabled="{TemplateBinding IsScrollInertiaEnabled}" + IsVisible="{TemplateBinding IsEmptyDataSource, Converter={x:Static BoolConverters.Not}}"> + + + + +