diff --git a/samples/TreeDataGridDemo/MainWindow.axaml b/samples/TreeDataGridDemo/MainWindow.axaml index c3f192f7..4849727d 100644 --- a/samples/TreeDataGridDemo/MainWindow.axaml +++ b/samples/TreeDataGridDemo/MainWindow.axaml @@ -28,7 +28,9 @@ + AutoDragDropRows="True" + CellValueChanged="TreeDataGrid_CellValueChanged" + RowPrepared="TreeDataGrid_RowPrepared"> diff --git a/samples/TreeDataGridDemo/MainWindow.axaml.cs b/samples/TreeDataGridDemo/MainWindow.axaml.cs index 883cac22..27d47209 100644 --- a/samples/TreeDataGridDemo/MainWindow.axaml.cs +++ b/samples/TreeDataGridDemo/MainWindow.axaml.cs @@ -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; @@ -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; + } } } diff --git a/samples/TreeDataGridDemo/Models/Country.cs b/samples/TreeDataGridDemo/Models/Country.cs index c64bdfa4..ae39c0b8 100644 --- a/samples/TreeDataGridDemo/Models/Country.cs +++ b/samples/TreeDataGridDemo/Models/Country.cs @@ -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 diff --git a/samples/TreeDataGridDemo/ViewModels/CountriesPageViewModel.cs b/samples/TreeDataGridDemo/ViewModels/CountriesPageViewModel.cs index 2be1344c..dde1666e 100644 --- a/samples/TreeDataGridDemo/ViewModels/CountriesPageViewModel.cs +++ b/samples/TreeDataGridDemo/ViewModels/CountriesPageViewModel.cs @@ -10,14 +10,13 @@ namespace TreeDataGridDemo.ViewModels { internal class CountriesPageViewModel : ReactiveObject { - private readonly ObservableCollection _data; private bool _cellSelection; public CountriesPageViewModel() { - _data = new ObservableCollection(Countries.All); + Data = new ObservableCollection(Countries.All); - Source = new FlatTreeDataGridSource(_data) + Source = new FlatTreeDataGridSource(Data) { Columns = { @@ -55,18 +54,14 @@ public bool CellSelection } } + public ObservableCollection Data { get; } public FlatTreeDataGridSource 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"; } } }