UWP - There are "ContentControl" items in GridView, when do "Drag and Drop", the item will be disappeared.

Zack Hou 26 Reputation points
2022-07-07T03:45:26.26+00:00

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.
218414-20220707-101036.gif

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

Developer technologies Universal Windows Platform (UWP)
0 comments No comments
{count} votes

Accepted answer
  1. Nico Zhu (Shanghai Wicresoft Co,.Ltd.) 12,866 Reputation points
    2022-07-07T06:29:42.613+00:00

    I have tested that use ContentControl to repace ContentPresenter in your previous case, It's still a binding issue that place the control in the model class.
    The solutuion base on your previous case is that replace ItemsWrapGrid with WrapGrid (still use ContentPresenter as container), the following line could disable virtualization behavior and make drag and drop avaiable for ContentPresenter

    <ItemsPanelTemplate x:Name="clu">  
        <WrapGrid Orientation="Horizontal"/>  
    </ItemsPanelTemplate>  
    

    Thanks
    Nico


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.