【问题标题】:UWP - c # Move json objects (nodes) to another object by drag and drop using ListView?UWP - c# 使用ListView通过拖放将json对象(节点)移动到另一个对象?
【发布时间】:2020-01-10 06:05:46
【问题描述】:

我正在开发一个 uwp 应用程序,我从 json 加载数据并将其保存在那里 我想要的是将父对象内的子对象移动到另一个父对象 通过界面列表视图的拖放功能

我正在使用 ObservableCollection 使用 foreach 迭代它们并将数据加载到列表中 使用 listview.ItemsSoure。

我有很多疑问,例如: * 如何知道我拖的是什么项目? * 如何在目标列表视图中检测我正在释放项目、对象、observableCollection? * 如何将子对象移动到父对象 但我不知道从哪里开始 我什至不知道我的计划是正确还是行不通

我的文件 json 数据:

{
  "date": 737282,
  "notebooks": [
    {
      "title": "Bienvenido",
      "created": 0,
      "notes": [
        {
          "title": "una nota que ya funciona ",
          "id": "SoYnXQslbxDTe",
          "created": 737307,
          "updated": 737309,
          "text": "de forma excepcional"
        },
        {
          "title": "agregar nuevas notas mas eficaces cada vez mas rapidos this is great",
          "id": "HGHQVRgpuvqrqs",
          "created": 737307,
          "updated": 737309,
          "text": ""
        },
        {
          "title": "intrensent and lorem ipsum dolor camen argon ersom",
          "id": "plwjWxOGaHhpg",
          "created": 737307,
          "updated": 737309,
          "text": "indelimit arkenson auk kalemer amred yut hyt"
        } 
      ],
      "id": "section1",
      "updated": 0
    },
    {
      "title": "bnfgdgh",
      "created": 737307,
      "notes": [
        {
          "title": "Bienvenido a la nueva version",
          "id": "eoERbgGDrdxDz",
          "created": 737307,
          "updated": 737309,
          "text": "welcome to new note sample"
        },
        {
          "title": "un item que no puedes reordenar",
          "id": "VSyL_HaV-tdEWz",
          "created": 737307,
          "updated": 737309,
          "text": "gdhgdh"
        }
      ],
      "id": "jwhsEbLMF",
      "updated": 737307
    },
    {
      "title": "Faus inside order",
      "created": 737307,
      "notes": [
        {
          "title": "mi nueva nota",
          "id": "qX-jM_s",
          "created": 737307,
          "updated": 737309,
          "text": ""
        },
        {
          "title": "dfdfd",
          "id": "CDTDjNLQ_",
          "created": 737307,
          "updated": 737309,
          "text": "dfgdfgdf"
        },
        {
          "title": "dfdfd",
          "id": "zhhskohLLEYh",
          "created": 737307,
          "updated": 737309,
          "text": ""
        } 
      ],
      "id": "muNVWlFNbyYdgP",
      "updated": 737307
    }
  ],
  "version": 1
}

我的模型类:

public class Notes : INotifyPropertyChanged
    {
        private string Title;
        public string title
        {
            get {return Title; }
            set
            {
                Title = value;
                NotifyPropertyChanged("title");
            }

            }
        public string id { get; set; }
        public int created { get; set; }
        public int updated { get; set; }
        public string text { get; set; }

        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this,new PropertyChangedEventArgs(propertyName));
            }
        }
    }
    public class Notebook
    {
        public string title { get; set; }
        public int created { get; set; }
        public List<Notes> notes { get; set; }
        public string id { get; set; }
        public int updated { get; set; }
    }
    public class Libro
    {
        public int date { get; set; }
        public List<Notebook> notebooks { get; set; }
        public int version { get; set; }
    }

我要移动的列表视图:

<ListView

                                x:Name="ListaNotas"
                               CanDragItems="True"
                                CanReorderItems="True"
                                AllowDrop="True"
                                IsItemClickEnabled="True"
                                ItemsSource="{Binding Mynotes}"

                                SelectedItem="{Binding SelectItem, Mode=TwoWay}"/>

我的列表视图放置目标:

<ListView
                            x:Name="listaSections"
                            Margin="0"
                            Padding="0"
                            AllowDrop="True"
                            CanReorderItems="True"
                            CanDragItems="True"
                            IsItemClickEnabled="True"
/>

我的问题: https://i.imgur.com/5QTZZHV.gif

我的目标: https://i.imgur.com/Go8nnrH.gif

【问题讨论】:

    标签: c# json xaml listview uwp


    【解决方案1】:

    如何知道我拖的是什么项目?

    ​ 如果你想知道你拖的是什么项目,你需要在你的 ListaNotas ListView 中订阅 DragItemsStarting 事件。它发生在拖动操作开始时。

    private void ListaNotas_DragItemsStarting(object sender, DragItemsStartingEventArgs e)
    {
        viewmodel dragItem = e.Items[0] as viewmodel;
    }
    

    如何在目标列表视图中检测我正在释放一个项目, 对象,observableCollection?

    listaSections ListView 中,不应将 AllowDrop 设置为 true。在这种情况下,您拖动的项目将被添加到 listView 而不是 listViewItem。所以你应该在子视图中设置 AllowDrop。例如,假设你的listViewItem中只有一个textBlock,那么你需要设置AllowDrop,并订阅DropDragEnter事件。当你释放你的item时,会触发Drop事件,你可以从中获取viewmodel。

    .xaml

    <ListView x:Name="listaSections"
                                Margin="0"
                                Padding="0"
                                CanReorderItems="True"
                                IsItemClickEnabled="True"
                                ItemsSource="{x:Bind RightLib.notebooks}" >
        <ListView.ItemTemplate>
            <DataTemplate x:DataType="local:Notebook">
                <TextBlock Text="{Binding title}" AllowDrop="True" Drop="TextBlock_Drop" DragEnter="TextBlock_DragEnter"></TextBlock>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
    

    .cs

    private void SongsListView_Drop(object sender, DragEventArgs e)
    {
        if ((e.OriginalSource as TextBlock)?.DataContext is Notebook targetAccount) {
            //targetAccount is Notebook 
        }
    }
    private void TextBlock_DragEnter(object sender, DragEventArgs e)
    {
        e.AcceptedOperation = DataPackageOperation.Move;
        e.DragUIOverride.Caption = "Move";
    }
    

    【讨论】:

    • 成功了,虽然实现了很久,最后还是成功了,再次感谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-20
    • 2014-03-14
    • 1970-01-01
    相关资源
    最近更新 更多