Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DataGrid: expose row index #15909

Merged
merged 2 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
rabbitism marked this conversation as resolved.
Show resolved Hide resolved
{
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
Loading