Hello,
I implemented "drag and drop" and "ContentControl" on GridView and used "Canreorder=True" property to change item's position.
When the first of "drag and drop", the item will be disappeared. Then, the second of "drag and drop", it will return to normal.
Because I need to use a instance object to do something in ViewModel, "ContentControl" is my way. Or is any else better way?
MainPage.xaml
<GridView
x:Name="grid"
Margin="100,10,0,10"
AllowDrop="True"
CanDragItems="True"
CanReorderItems="True"
IsSwipeEnabled="True"
ItemsSource="{x:Bind ViewModel.PreviewItems}">
<GridView.ItemTemplate>
<DataTemplate>
<Grid
Width="340"
Height="240"
Padding="5,0,5,0"
BorderThickness="1">
<Grid.RowDefinitions>
<RowDefinition Height="50" />
<RowDefinition Height="180" />
</Grid.RowDefinitions>
<ContentControl
Grid.Row="1"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Content="{Binding ImageThumbnail}" />
</Grid>
</DataTemplate>
</GridView.ItemTemplate>
<GridView.ItemsPanel>
<ItemsPanelTemplate x:Name="clu">
<ItemsWrapGrid MaximumRowsOrColumns="5" Orientation="Horizontal" />
</ItemsPanelTemplate>
</GridView.ItemsPanel>
</GridView>
MainViewModel.cs
public class MainViewModel : ObservableObject
{
public ObservableCollection<PreviewItem> PreviewItems = new ObservableCollection<PreviewItem>();
public MainViewModel()
{
for (int i = 0; i < 2; i++)
{
var item = new PreviewItem();
item.ImageThumbnail = new Windows.UI.Xaml.Controls.Image();
ImageSource result = new BitmapImage(new Uri("ms-appx:///Assets/Square150x150Logo.png"));
item.ImageThumbnail.Source = result;
PreviewItems.Add(item);
}
}
}
PreviewItem.cs
public class PreviewItem : ObservableObject
{
private Image _imageThumbnail;
public Image ImageThumbnail { get => _imageThumbnail; set => SetProperty(ref _imageThumbnail, value); }
public PreviewItem()
{
}
}
Here is my source code.
https://github.com/houzhiwei/UWPProject