Skip to content

Commit

Permalink
DataGrid: expose row index (#15909)
Browse files Browse the repository at this point in the history
* feat: expose index.

* feat: make Index a direct property. obsolete get method. Update sample.
  • Loading branch information
rabbitism committed Jun 6, 2024
1 parent e7819cc commit 7413434
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
5 changes: 5 additions & 0 deletions samples/ControlCatalog/Pages/DataGridPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -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" />
Expand Down
6 changes: 0 additions & 6 deletions samples/ControlCatalog/Pages/DataGridPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ public DataGridPage()
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;
Expand Down Expand Up @@ -64,11 +63,6 @@ public DataGridPage()
}

public IEnumerable<Person> DataGrid3Source { get; }

private void Dg1_LoadingRow(object? sender, DataGridRowEventArgs e)
{
e.Row.Header = e.Row.GetIndex() + 1;
}

private void InitializeComponent()
{
Expand Down
12 changes: 9 additions & 3 deletions src/Avalonia.Controls.DataGrid/DataGridRow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -220,13 +220,18 @@ internal DataGrid OwningGrid
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
Expand Down Expand Up @@ -435,6 +440,7 @@ internal double TargetHeight
/// <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;
Expand Down

0 comments on commit 7413434

Please sign in to comment.