Moving Listview Item up or down
-
Hi I'm trying to find how to move a listview item up or down (using MVVM) in the collection and to change the data bound field. The problem is not the selected item Order being changed but the item that it's moved in front or behind being changed. A simple example
<UserControl x:Class="Views.TestPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"><DockPanel > <StackPanel Orientation="Horizontal" DockPanel.Dock="Top"> <Button Content="Up" Command="{Binding Path=UpCommand}" /> <Button Content="Down" Command="{Binding Path=DownCommand}" /> </StackPanel> <ListView ItemsSource="{Binding Path=CurrentList}" IsSynchronizedWithCurrentItem="True"> <ListView.View> <GridView> <GridViewColumn Header="Order" DisplayMemberBinding="{Binding Path=OrderPriority}"/> <GridViewColumn Header="Question" DisplayMemberBinding="{Binding Path=Question}" /> </GridView> </ListView.View> </ListView> </DockPanel>
</UserControl>
Model
namespace Models
{
public class myTable
{
public Int32 OrderPriority { get; set; }
public String Question { get; set; }
}
}I don't really want to bombard anybody with viewmodel code, but lets just say I have all the necessary bindings that are needed. When the user clicks the Up button it executes the following
public void ExecuteUpCommand() { UpCommand.CommandSucceeded = false; //The collection is stored in myCollectionView //The selected ListViewItem is stored as CurrentItem (of type myTable), //retrieved through myCollectionView\_CurrentChanged //Do I use myCollectionView.MoveCurrentToPrevious();
//Update current item with new Order number serviceClient.UpdateMyTable(CurrentItem)
//Update the replaced item serviceClient.UpdateMyTable()??UpCommand.CommandSucceeded = true; }
What do I do here?
-
Hi I'm trying to find how to move a listview item up or down (using MVVM) in the collection and to change the data bound field. The problem is not the selected item Order being changed but the item that it's moved in front or behind being changed. A simple example
<UserControl x:Class="Views.TestPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"><DockPanel > <StackPanel Orientation="Horizontal" DockPanel.Dock="Top"> <Button Content="Up" Command="{Binding Path=UpCommand}" /> <Button Content="Down" Command="{Binding Path=DownCommand}" /> </StackPanel> <ListView ItemsSource="{Binding Path=CurrentList}" IsSynchronizedWithCurrentItem="True"> <ListView.View> <GridView> <GridViewColumn Header="Order" DisplayMemberBinding="{Binding Path=OrderPriority}"/> <GridViewColumn Header="Question" DisplayMemberBinding="{Binding Path=Question}" /> </GridView> </ListView.View> </ListView> </DockPanel>
</UserControl>
Model
namespace Models
{
public class myTable
{
public Int32 OrderPriority { get; set; }
public String Question { get; set; }
}
}I don't really want to bombard anybody with viewmodel code, but lets just say I have all the necessary bindings that are needed. When the user clicks the Up button it executes the following
public void ExecuteUpCommand() { UpCommand.CommandSucceeded = false; //The collection is stored in myCollectionView //The selected ListViewItem is stored as CurrentItem (of type myTable), //retrieved through myCollectionView\_CurrentChanged //Do I use myCollectionView.MoveCurrentToPrevious();
//Update current item with new Order number serviceClient.UpdateMyTable(CurrentItem)
//Update the replaced item serviceClient.UpdateMyTable()??UpCommand.CommandSucceeded = true; }
What do I do here?
I think I've found the answer but if you can think of a better way please let me know
Int32 currentPosition = myCollectionView.CurrentPosition;
if (currentPosition > 0)
{
CurrentItem.OrderPriority = currentPosition;myCollectionView.MoveCurrentToPrevious(); CurrentItem.OrderPriority = CurrentItem.OrderPriority + 1;
}
myCollectionView.Refresh();