Use ListBox instead of ItemsRepeater

This commit is contained in:
Wiesław Šoltés 2021-08-11 10:17:16 +02:00
Родитель fc710d39c8
Коммит 3e9c54f1a5
3 изменённых файлов: 57 добавлений и 5 удалений

Просмотреть файл

@ -11,6 +11,7 @@
<!-- DataGridColumnHeadersPresenter -->
<Grid Name="PART_ColumnHeaders" />
<!-- DataGridRowsPresenter -->
<!-- NOTE: If !USE_LISTBOX
<ScrollViewer Name="PART_ScrollViewer">
<ItemsRepeater Name="PART_ItemsRepeater" Items="{TemplateBinding Items}">
<ItemsRepeater.Layout>
@ -18,6 +19,26 @@
</ItemsRepeater.Layout>
</ItemsRepeater>
</ScrollViewer>
-->
<ListBox Name="PART_ListBox" Items="{TemplateBinding Items}" SelectedItem="{TemplateBinding SelectedItem}">
<ListBox.Styles>
<Style Selector="ListBox">
<Setter Property="Background" Value="Transparent"/>
</Style>
</ListBox.Styles>
<ListBox.Styles>
<Style Selector="ListBoxItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
<Setter Property="Margin" Value="0"/>
<Setter Property="Padding" Value="0"/>
</Style>
</ListBox.Styles>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel Orientation="Vertical"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
</Grid>
</ControlTemplate>
</Setter>

Просмотреть файл

@ -1,4 +1,5 @@
using System.Collections;
#define USE_LISTBOX
using System.Collections;
using System.Collections.Generic;
using Avalonia;
using Avalonia.Collections;
@ -14,9 +15,12 @@ namespace ItemsRepeaterDataGrid
private List<Control> _rootChildren = new List<Control>();
private Grid? _columnHeaders;
private List<Control> _columnHeadersChildren = new List<Control>();
#if !USE_LISTBOX
private ScrollViewer? _scrollViewer;
private ItemsRepeater? _itemsRepeater;
#else
private ListBox? _listBox;
#endif
public static readonly StyledProperty<AvaloniaList<DataGridColumn>> ColumnsProperty =
AvaloniaProperty.Register<DataGrid, AvaloniaList<DataGridColumn>>(nameof(Columns), new AvaloniaList<DataGridColumn>());
@ -59,9 +63,12 @@ namespace ItemsRepeaterDataGrid
_root = e.NameScope.Find<Grid>("PART_Root");
_columnHeaders = e.NameScope.Find<Grid>("PART_ColumnHeaders");
#if !USE_LISTBOX
_scrollViewer = e.NameScope.Find<ScrollViewer>("PART_ScrollViewer");
_itemsRepeater = e.NameScope.Find<ItemsRepeater>("PART_ItemsRepeater");
#else
_listBox = e.NameScope.Find<ListBox>("PART_ListBox");
#endif
InvalidateRoot();
InvalidateColumnHeaders();
InvalidateScrollViewer();
@ -138,7 +145,7 @@ namespace ItemsRepeaterDataGrid
_rootChildren.Add(horizontalSeparator);
// Set ItemsRepeater template
#if !USE_LISTBOX
if (_itemsRepeater is { })
{
_itemsRepeater.ItemTemplate = new FuncDataTemplate<object>(
@ -147,7 +154,16 @@ namespace ItemsRepeaterDataGrid
[!DataGridRow.ColumnsProperty] = this[!DataGrid.ColumnsProperty]
});
}
#else
if (_listBox is { })
{
_listBox.ItemTemplate = new FuncDataTemplate<object>(
(_, _) => new DataGridRow()
{
[!DataGridRow.ColumnsProperty] = this[!DataGrid.ColumnsProperty]
});
}
#endif
foreach (var columnIndex in splitterColumnIndexes)
{
// Generate Root Horizontal Separator's
@ -234,6 +250,7 @@ namespace ItemsRepeaterDataGrid
private void InvalidateScrollViewer()
{
#if !USE_LISTBOX
if (_scrollViewer is null)
{
return;
@ -244,6 +261,18 @@ namespace ItemsRepeaterDataGrid
_scrollViewer.SetValue(Grid.RowProperty, 2);
_scrollViewer.SetValue(Grid.ColumnProperty, 0);
_scrollViewer.SetValue(Grid.ColumnSpanProperty, columns.Count + (columns.Count - 1));
#else
if (_listBox is null)
{
return;
}
var columns = Columns;
_listBox.SetValue(Grid.RowProperty, 2);
_listBox.SetValue(Grid.ColumnProperty, 0);
_listBox.SetValue(Grid.ColumnSpanProperty, columns.Count + (columns.Count - 1));
#endif
}
}
}

Просмотреть файл

@ -11,10 +11,12 @@
</ControlTemplate>
</Setter>
</Style>
<!-- NOTE: If !USE_LISTBOX
<Style Selector="g|DataGridRow /template/ Grid#PART_Root">
<Setter Property="Background" Value="Transparent" />
</Style>
<Style Selector="g|DataGridRow:pointerover /template/ Grid#PART_Root">
<Setter Property="Background" Value="LightGray" />
</Style>
-->
</Styles>