Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 更改ViewModel中ObservableCollection的属性而不选择它_C#_Wpf_Mvvm_Treeview_Observablecollection - Fatal编程技术网

C# 更改ViewModel中ObservableCollection的属性而不选择它

C# 更改ViewModel中ObservableCollection的属性而不选择它,c#,wpf,mvvm,treeview,observablecollection,C#,Wpf,Mvvm,Treeview,Observablecollection,在我的程序中,我有一个TreeView,它是通过一个ViewModel通过使用ObservableCollection实现的。每个集合都有一个名为Rank的属性。这应该用作集合项的索引 在中,我能够使用observeCollection.Move()让我的TreeView节点切换位置 但是,在切换节点的位置后,我需要更正/更改节点秩的值,以便继续操作它们 这应该有助于解释我在做什么: 视图--代码隐藏: //Button Click Event -- This makes the Selecte

在我的程序中,我有一个
TreeView
,它是通过一个ViewModel通过使用
ObservableCollection
实现的。每个集合都有一个名为
Rank
的属性。这应该用作集合项的索引

在中,我能够使用
observeCollection.Move()让我的
TreeView
节点切换位置

但是,在切换节点的位置后,我需要更正/更改节点秩的值,以便继续操作它们

这应该有助于解释我在做什么:

视图--代码隐藏:

//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;
       }
   }
}
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(...);
     }
}

您可以更改对象列组上的实际值(假设它们是公共属性),并通过绑定自动完成排序,而不是实际移动项目

编辑: 这很尴尬,正如你的评论所说,我的链接是winforms

话虽如此,看看这个精彩的答案,然后自我回答

他们要么实现了一个比较器(我将使用msdn链接的方向),要么使用默认的头

这将给你一个很好的开端,并为你的排名树立榜样

编辑

在Xaml上:

<StackPanel x:Name="LayoutRoot">
<TreeView Name="TestTreeView" ItemsSource="{Binding MyTree}">
    <TreeView.ItemTemplate>
        <DataTemplate>
            <Border BorderBrush="Black" BorderThickness="1" Margin="2" Padding="2">
            <TextBlock Text="{Binding Path=Name}"/>
            </Border>
        </DataTemplate>
    </TreeView.ItemTemplate>
</TreeView>
<Button Command="{Binding SortMe_Command}">Sort</Button>
我已经添加了rachel的类:

public class SortableObservableCollection<T> : ObservableCollection<T> { ...}
现在,当我点击按钮,它将改变2个项目的排名,并根据排名诉诸树


在您的应用程序中,只需找出您要交换的是哪一个,然后设置…

它们是在
TreeView
的模型中声明的公共属性,但是更改
Rank
如何允许它们自动重新排序?我这样问是因为我创建了
Rank
。它不会自动转换为集合索引。如果你提供自己的TreeViewNodeSorter,你可以按照你想要的方式对树进行排序。那篇文章是针对Windows窗体的。我编辑了我的问题。我喜欢“瑞秋”这个答案。我理解它,但我很难理解如何为我的程序实现它。请看一看,如果你能帮忙,请告诉我。非常感谢。
public class SortableObservableCollection<T> : ObservableCollection<T> { ...}
public SortableObservableCollection<MyTreeClass> MyTree
    {
        get { return _myTree; }
        set { _myTree = value; }
    }
private SortableObservableCollection<MyTreeClass> _myTree;
MyTree = new SortableObservableCollection<MyTreeClass>() {new MyTreeClass(){Name = "One",Rank = 1},
        new MyTreeClass(){Name = "Two",Rank = 2},
        new MyTreeClass(){Name = "Three",Rank = 3}};
        SortMe_Command = new RelayCommand<object>(Execute_SortMe);
SortMe_Command = new RelayCommand<object>(Execute_SortMe);
private void Execute_SortMe(object obj)
    {
        MyTree[0].Rank = 5;
        MyTree[1].Rank = 4;

        MyTree.Sort(node => node.Rank);
    }