【发布时间】:2015-10-21 20:36:47
【问题描述】:
我正在尝试制作一个带有行号的 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?
【问题讨论】:
标签: c# wpf listview data-binding