在我的程序中,我有一个TreeView
通过 ViewModel 使用ObservableCollection
. 每个集合都有一个名为 的属性Rank
。这应该用作集合项的索引。
在这个问题中,我能够让我的TreeView
节点使用ObservableCollection.Move();
但是,在切换节点的位置后,我需要更正/更改节点排名的值,以便我可以继续操作它们。
这应该有助于解释我在做什么:
查看 -- 代码隐藏:
//Button Click Event -- This makes the Selected Node switch places with the node above it
private void shiftUp_Click(object sender, RoutedEventArgs e)
{
//if a node is selected
if (UCViewModel.TreeViewViewModel.SelectedItem != null)
{
//If the selected Node is not in the 0 position (can not move up anymore)
if (UCViewModel.TreeViewViewModel.Collection<TreeViewModel>.IndexOf(UCViewModel.TreeViewViewModel.SelectedItem) != 0)
{
int oldIndex = UCViewModel.TreeViewViewModel.SelectedItem.Rank;
int newIndex = oldIndex--;
UCViewModel.TreeViewViewModel.Collection<TreeViewModel>.Move(oldIndex, newIndex);
//**Pseudo code trying to explain what I want to do
//**get item at specific index and change the Rank value
//Collection item at index (newIndex).Rank -= 1;
//Collection item at index (oldIndex).Rank += 1;
}
}
}
用户控件——XAML:
<TreeView ItemsSource="{Binding TreeViewViewModel.Collection<TreeModel>}" ... />
移动后如何更Rank
正值?
编辑
如上所述,我Rank
的TreeView
. @Noctis 的回答建议在值更改TreeView
后使用该属性对我进行排序。Rank
我最喜欢的关于这个主题的问题证明了这一点,here。
我已经将该SortObservableCollection
类添加到我的程序中,所以现在剩下的就是操作排名值和排序。执行此操作的正确位置是否来自代码隐藏?基本上上面的^部分来自哪里?如果是这样的话,我对确切的电话有点困惑......
代码隐藏:
private void shiftUp_Click(object sender, RoutedEventArgs e)
{
//if a node is selected
if (UCViewModel.TreeViewViewModel.SelectedItem != null)
{
//Moves the selectedNode down one (Up visually, hence shiftUp)
UCViewModel.TreeViewViewModel.SelectedItem.Rank--;
//How would I get the node below the selected one and change the Rank?
//This would be the call to sort. Which needs to be called for the collection
//For some reason, sort does not come up for the collection...
//UCViewModel.TreeViewViewModel.Collection.**Sort(...);
}
}