代码之家  ›  专栏  ›  技术社区  ›  EldHasp

添加到空集合时AlternationIndex不正确

  •  0
  • EldHasp  · 技术社区  · 3 年前

    重现问题的一个简单示例:

    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>
    

    当通过直接在DataGrid中键入新行将元素添加到空集合中时,AlternationIndex值从 AlternationCount - 1 。
    enter image description here

    如果对集合视图进行刷新,则会恢复正确的AlternationIndex计算。
    enter image description here

    清理完集合后,一切都以相同的方式重复。

    有办法解决这个问题吗?

    附言:以防万一,我澄清一下,我不需要源数据中的行号。只有在视图中才需要它,以方便用户使用。

    0 回复  |  直到 3 年前
    推荐文章