0

A simple example to reproduce the problem:

namespace IncorrectAlternativeIndex
{
    /// <summary>A simple class to demonstrate the problem</summary>
    public class Point2D
    {
        public double X { get; set; }
        public double Y { get; set; }

    }
}
namespace IncorrectAlternativeIndex
{
    /// <summary>A simple ViewModel to demonstrate the problem</summary>
    public class PointsViewModel : ViewModelBase
    {
        public ObservableCollection<Point2D> Points { get; } = new();

        public RelayCommand ClearPoints => GetCommand(Points.Clear);

        /// <summary>This command should not exist.
        /// I brought it only to demonstrate the problem. </summary>
        public RelayCommand Refresh => GetCommand<CollectionView>(cv => cv.Refresh());
    }
}
<Window x:Class="IncorrectAlternativeIndex.CheckAlternativeIndexWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:IncorrectAlternativeIndex"
        xmlns:sys="clr-namespace:System;assembly=netstandard"
        mc:Ignorable="d"
        Title="CheckAlternativeIndexWindow" Height="450" Width="800"
        DataContext="{DynamicResource vm}">
    <Window.Resources>
        <local:PointsViewModel x:Key="vm"/>
    </Window.Resources>
    <UniformGrid Rows="1">
        <DataGrid x:Name="dGrid" ItemsSource="{Binding Points}"
                  AlternationCount="{x:Static sys:Int32.MaxValue}">
            <DataGrid.RowHeaderTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Path=(ItemsControl.AlternationIndex),
                                              RelativeSource={RelativeSource FindAncestor,
                                                                             AncestorType=DataGridRow}}"/>
                </DataTemplate>
            </DataGrid.RowHeaderTemplate>
        </DataGrid>
        <UniformGrid Columns="1">
            <Button Content="Clear" Margin="50" Command="{Binding ClearPoints}"/>
            <Button Content="Refresh" Margin="50" Command="{Binding Refresh}"
                    CommandParameter="{Binding Items, ElementName=dGrid}"/>
        </UniformGrid>
    </UniformGrid>
</Window>

When adding elements to an empty collection by typing into a new row directly in the DataGrid, the AlternationIndex value starts at AlternationCount - 1.
enter image description here

If do a Refresh of the collection view, the correct AlternationIndex calculation is restored.
enter image description here

After cleaning the collection, everything is repeated in the same way.

Is there a way to fix this?

P.S. Just in case, I clarify that I do not need line numbers in the source data. It is required only in the View for the convenience of the user.

EldHasp
  • 6,079
  • 2
  • 9
  • 24

0 Answers0