Answered by:
How to move or swap two GridView items?

Question
-
Hi,
am developing a windows store 8.1 app.
I have four GridView items in the GridView like the above figure.
Now i want to swap the GridView Items.
Now i click on 1(GridView Item) and then later i click on 4(Grid View Item)
Then both items has to be swap(4 should be replaced by 1 and 1 should be replaced by 4).
How can i achieve this?
I don't wanna use CanReorderItems="True" AllowDrop="True" CanDragItems="True" these properties to swap.
Any help please?
Wednesday, December 24, 2014 11:09 AM
Answers
-
<Page x:Class="GridViewItemSwap.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:GridViewItemSwap" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"> <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> <GridView x:Name="gridview" IsItemClickEnabled="True" ItemClick="gridview_ItemClick" > <GridView.ItemTemplate > <DataTemplate > <Border Margin="12" Width="100" Height="100" Background="Blue" > <TextBlock Text="{Binding}"/> </Border> </DataTemplate> </GridView.ItemTemplate> </GridView> </Grid> </Page>
namespace GridViewItemSwap { /// <summary> /// An empty page that can be used on its own or navigated to within a Frame. /// </summary> public sealed partial class MainPage : Page { private int? firstvalue; private ObservableCollection<Int32> data = new ObservableCollection<int>(); public MainPage() { this.InitializeComponent(); for(int i =0 ; i <4; i++) { data.Add(i); } gridview.ItemsSource = data; } private void gridview_ItemClick(object sender, ItemClickEventArgs e) { if (firstvalue.HasValue ) { Int32 secondvalue = (Int32)e.ClickedItem; var firstindex= data.IndexOf(firstvalue.Value); var secondindex = data.IndexOf(secondvalue); data[firstindex] = secondvalue; data[secondindex] = firstvalue.Value ; firstvalue = null; } else { firstvalue = (Int32)e.ClickedItem; } } } }
在現實生活中,你和誰在一起的確很重要,甚至能改變你的成長軌跡,決定你的人生成敗。 和什麼樣的人在一起,就會有什麼樣的人生。 和勤奮的人在一起,你不會懶惰; 和積極的人在一起,你不會消沈; 與智者同行,你會不同凡響; 與高人為伍,你能登上巔峰。
- Marked as answer by Jamles HezModerator Wednesday, January 7, 2015 9:45 AM
Wednesday, December 24, 2014 3:19 PM
All replies
-
<Page x:Class="GridViewItemSwap.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:GridViewItemSwap" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"> <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> <GridView x:Name="gridview" IsItemClickEnabled="True" ItemClick="gridview_ItemClick" > <GridView.ItemTemplate > <DataTemplate > <Border Margin="12" Width="100" Height="100" Background="Blue" > <TextBlock Text="{Binding}"/> </Border> </DataTemplate> </GridView.ItemTemplate> </GridView> </Grid> </Page>
namespace GridViewItemSwap { /// <summary> /// An empty page that can be used on its own or navigated to within a Frame. /// </summary> public sealed partial class MainPage : Page { private int? firstvalue; private ObservableCollection<Int32> data = new ObservableCollection<int>(); public MainPage() { this.InitializeComponent(); for(int i =0 ; i <4; i++) { data.Add(i); } gridview.ItemsSource = data; } private void gridview_ItemClick(object sender, ItemClickEventArgs e) { if (firstvalue.HasValue ) { Int32 secondvalue = (Int32)e.ClickedItem; var firstindex= data.IndexOf(firstvalue.Value); var secondindex = data.IndexOf(secondvalue); data[firstindex] = secondvalue; data[secondindex] = firstvalue.Value ; firstvalue = null; } else { firstvalue = (Int32)e.ClickedItem; } } } }
在現實生活中,你和誰在一起的確很重要,甚至能改變你的成長軌跡,決定你的人生成敗。 和什麼樣的人在一起,就會有什麼樣的人生。 和勤奮的人在一起,你不會懶惰; 和積極的人在一起,你不會消沈; 與智者同行,你會不同凡響; 與高人為伍,你能登上巔峰。
- Marked as answer by Jamles HezModerator Wednesday, January 7, 2015 9:45 AM
Wednesday, December 24, 2014 3:19 PM -
Hey Chung,
Thanks for your reply..
Monday, December 29, 2014 5:49 AM