gpt4 book ai didi

c# - WPF AlternationIndex 在将项目移动到列表顶部时环绕

转载 作者:太空宇宙 更新时间:2023-11-03 10:21:38 24 4
gpt4 key购买 nike

我正在尝试制作一个带有行号的 GridView, 为用户提供向上/向下按钮以更改行的顺序。许多帖子建议使用 AlternationIndex(例如 here)并且几乎 工作得很好,并且当用户按下向上/向下按钮以更改行的顺序时它会处理。但是,当您将项目移动到第一个位置时,AlternationIndex 会失败——此时,它应该显示 0,但它会环绕到 AlternationCount 中的最后一个值。

例子:

    <ListView AlternationCount="1000" Name="_stuff" Grid.Row="0">
<ListView.View>
<GridView>
<GridViewColumn
Header="Alit" Width="30"
DisplayMemberBinding="{Binding (ItemsControl.AlternationIndex),
RelativeSource={RelativeSource AncestorType=ListViewItem}}" />

<GridViewColumn Header="ColumnName" DisplayMemberBinding="{Binding}" Width="240"/>
</GridView>
</ListView.View>
</ListView>

然后我的代码隐藏是:

 ObservableCollection<string> data = new ObservableCollection<string>() { "First", "Second", "Third", "Forth", "Last" };

public MainWindow()
{
InitializeComponent();
_stuff.ItemsSource = data;
}

private void UpDownButton_Click(object sender, RoutedEventArgs e)
{
//User wants to change the order -- remove the item from the observable
//collection and reinsert it at the new position.
data.Remove("Last"); //First remove, then re-insert.

//If you move the last item to *middle* of list, it works fine and the index
//is correct. (All other items are pushed down by 1, like you'd expect.)
//But move to the *top* of the list and the new index is 999?

//THIS WOULD WORK FINE AND ALL INDEXES ARE CORRECT
// data.Insert(3, "Last"); //Insert to middle of list

//But this gives an index of 999???? Seems to be wrapping
//around to the last AlternationCount. But why?
data.Insert(0, "Last"); //insert to top of list
}

有什么想法可以使顶部新项目的 alternationIndex 为 0 而不是 999?

最佳答案

我认为这是一个错误。它的行为是 Not Acceptable 。不知怎么的,AltenrationIndex 设置错了,你可以找到关于这个的源代码,但坦率地说,它不是很容易理解。这背后有一些算法涉及虚拟化机制以及它们实现的模式。这是我认为可能涉及更新到 AlternationIndex 的源代码,方法 SetAlternationIndex .它在一些回调中被调用,例如当 AlternationCount 被更改时,项目被删除时,...

这里涉及到的比较难理解的概念有ItemBlockoffset以及GeneratorDirection。这就是为什么我很难理解那里的代码。

我发布这个答案是因为我找到了一个简单的解决方案,虽然我不确定它是否存在任何性能问题,但我很确定它应该是可以接受的。

如果重新生成项目容器,AlternationIndex 应该再次按顺序排列。因为在虚拟化模式下,项目容器的数量正好等于可见元素的数量,所以我认为这不会导致性能问题。 ItemContainerGenerator 实现接口(interface) IItemContainerGenerator,它有一个名为 RemoveAll 的方法。通过使用此方法,所有已实现(生成的)项目容器将被删除,它们将再次自动生成,从而导致 AlternationIndex 以正确的顺序设置。

另外,正如您所说,问题似乎只有在第一个索引处插入该项目时才会出现。所以我们只需要在特定情况下使用这个技巧:

private void UpDownButton_Click(object sender, RoutedEventArgs e)
{
//before changing any item at index 0 such as by inserting
//some new one or even use the method Move(... , 0),
//We need to clear all the realized item containers
var cg = _stuff.ItemContainerGenerator as IItemContainerGenerator;
cg.RemoveAll();
//now just proceed the code
//we can in fact use this instead data.Move(data.Count - 1, 0);
data.Remove("Last");
data.Insert(0, "Last");
}

这是另一个使用 ICollectionView.Refresh() 方法的漂亮解决方案。您只需要刷新它(但当然应该只在出现该问题时才应用):

private void UpDownButton_Click(object sender, RoutedEventArgs e)
{
//we can in fact use this instead data.Move(data.Count - 1, 0);
data.Remove("Last");
data.Insert(0, "Last");
CollectionViewSource.GetDefaultView(data).Refresh();
}

关于c# - WPF AlternationIndex 在将项目移动到列表顶部时环绕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33268757/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com