Browse Source

DataGrid: expose row index (#15909)

* feat: expose index.

* feat: make Index a direct property. obsolete get method. Update sample.
pull/15931/head
Dong Bin 2 years ago
committed by GitHub
parent
commit
7413434e87
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 5
      samples/ControlCatalog/Pages/DataGridPage.xaml
  2. 6
      samples/ControlCatalog/Pages/DataGridPage.xaml.cs
  3. 12
      src/Avalonia.Controls.DataGrid/DataGridRow.cs

5
samples/ControlCatalog/Pages/DataGridPage.xaml

@ -45,6 +45,11 @@
</StackPanel>
<DataGrid Name="dataGrid1" Margin="12" CanUserResizeColumns="True" CanUserReorderColumns="True" CanUserSortColumns="True" HeadersVisibility="All"
RowBackground="#1000">
<DataGrid.Styles>
<Style Selector="DataGridRow">
<Setter Property="Header" Value="{Binding $self.Index}"/>
</Style>
</DataGrid.Styles>
<DataGrid.Columns>
<!-- Using HeaderTemplate -->
<DataGridTextColumn Header="Country or Region" HeaderTemplate="{StaticResource Demo.DataTemplates.CountryHeader}" Binding="{Binding Name}" Width="6*" x:DataType="local:Country" />

6
samples/ControlCatalog/Pages/DataGridPage.xaml.cs

@ -24,7 +24,6 @@ namespace ControlCatalog.Pages
collectionView1.SortDescriptions.Add(dataGridSortDescription);
var dg1 = this.Get<DataGrid>("dataGrid1");
dg1.IsReadOnly = true;
dg1.LoadingRow += Dg1_LoadingRow;
dg1.Sorting += (s, a) =>
{
var binding = (a.Column as DataGridBoundColumn)?.Binding as Binding;
@ -64,11 +63,6 @@ namespace ControlCatalog.Pages
}
public IEnumerable<Person> DataGrid3Source { get; }
private void Dg1_LoadingRow(object? sender, DataGridRowEventArgs e)
{
e.Row.Header = e.Row.GetIndex() + 1;
}
private void InitializeComponent()
{

12
src/Avalonia.Controls.DataGrid/DataGridRow.cs

@ -220,13 +220,18 @@ namespace Avalonia.Controls
set;
}
private int _index;
public static readonly DirectProperty<DataGridRow, int> IndexProperty = AvaloniaProperty.RegisterDirect<DataGridRow, int>(
nameof(Index), o => o.Index, (o, v) => o.Index = v);
/// <summary>
/// Index of the row
/// </summary>
internal int Index
public int Index
{
get;
set;
get => _index;
internal set => SetAndRaise(IndexProperty, ref _index, value);
}
internal double ActualBottomGridLineHeight
@ -435,6 +440,7 @@ namespace Avalonia.Controls
/// <returns>
/// The index of the current row.
/// </returns>
[Obsolete("This API is going to be removed in a future version. Use the Index property instead.")]
public int GetIndex()
{
return Index;

Loading…
Cancel
Save