Skip to content

Commit

Permalink
Add a simple example of using CellValueChanged.
Browse files Browse the repository at this point in the history
Change the background color of a row with a country name staring with `A`.
  • Loading branch information
grokys committed Dec 13, 2024
1 parent ef0561f commit c927562
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 14 deletions.
4 changes: 3 additions & 1 deletion samples/TreeDataGridDemo/MainWindow.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
</StackPanel>
<TreeDataGrid Name="countries"
Source="{Binding Countries.Source}"
AutoDragDropRows="True">
AutoDragDropRows="True"
CellValueChanged="TreeDataGrid_CellValueChanged"
RowPrepared="TreeDataGrid_RowPrepared">
<TreeDataGrid.Resources>
<!-- Template for Region column cells -->
<DataTemplate x:Key="RegionCell" DataType="m:Country">
Expand Down
26 changes: 26 additions & 0 deletions samples/TreeDataGridDemo/MainWindow.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Avalonia.Interactivity;
using Avalonia.LogicalTree;
using Avalonia.Markup.Xaml;
using Avalonia.Media;
using Avalonia.Threading;
using Avalonia.VisualTree;
using TreeDataGridDemo.Models;
Expand Down Expand Up @@ -128,5 +129,30 @@ private void UpdateRealizedCount()
var unrealizedRowCount = rows.GetVisualChildren().Count() - realizedRowCount;
textBlock.Text = $"{realizedRowCount} rows realized ({unrealizedRowCount} unrealized)";
}

private void TreeDataGrid_RowPrepared(object? sender, TreeDataGridRowEventArgs e)
{
if (DataContext is not MainWindowViewModel vm)
return;

var modelIndex = vm.Countries.Source.Rows.RowIndexToModelIndex(e.RowIndex);
var model = vm.Countries.Data[modelIndex[0]];

e.Row.Background = model.Name?.StartsWith('A') == true ? Brushes.Yellow : null;
}

private void TreeDataGrid_CellValueChanged(object? sender, TreeDataGridCellEventArgs e)
{
if (DataContext is not MainWindowViewModel vm ||
sender is not TreeDataGrid tdg ||
e.ColumnIndex != 0 ||
tdg.TryGetRow(e.RowIndex) is not { } row)
return;

var modelIndex = vm.Countries.Source.Rows.RowIndexToModelIndex(e.RowIndex);
var model = vm.Countries.Data[modelIndex[0]];

row.Background = model.Name?.StartsWith('A') == true ? Brushes.Yellow : null;
}
}
}
15 changes: 12 additions & 3 deletions samples/TreeDataGridDemo/Models/Country.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
namespace TreeDataGridDemo.Models
using ReactiveUI;

namespace TreeDataGridDemo.Models
{
internal class Country
internal class Country : ReactiveObject
{
public string? Name { get; set; }
private string? _name;

public string? Name
{
get => _name;
set => this.RaiseAndSetIfChanged(ref _name, value);
}

public string Region { get; private set; }
public int Population { get; private set; }
//Square Miles
Expand Down
15 changes: 5 additions & 10 deletions samples/TreeDataGridDemo/ViewModels/CountriesPageViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,13 @@ namespace TreeDataGridDemo.ViewModels
{
internal class CountriesPageViewModel : ReactiveObject
{
private readonly ObservableCollection<Country> _data;
private bool _cellSelection;

public CountriesPageViewModel()
{
_data = new ObservableCollection<Country>(Countries.All);
Data = new ObservableCollection<Country>(Countries.All);

Source = new FlatTreeDataGridSource<Country>(_data)
Source = new FlatTreeDataGridSource<Country>(Data)
{
Columns =
{
Expand Down Expand Up @@ -55,18 +54,14 @@ public bool CellSelection
}
}

public ObservableCollection<Country> Data { get; }
public FlatTreeDataGridSource<Country> Source { get; }

public void AddCountry(Country country) => _data.Add(country);
public void AddCountry(Country country) => Data.Add(country);

public void RemoveSelected()
{
var selection = ((ITreeSelectionModel)Source.Selection!).SelectedIndexes.ToList();

for (var i = selection.Count - 1; i >= 0; --i)
{
_data.RemoveAt(selection[i][0]);
}
Data[0].Name = "Foo";
}
}
}

0 comments on commit c927562

Please sign in to comment.