【问题标题】:Drag & drop between Listbox items and chess board在列表框项目和棋盘之间拖放
【发布时间】:2012-07-23 07:53:06
【问题描述】:

我正在尝试找到一种使用 WPF 在列表框项目和棋盘之间拖放的方法。我在左边有一个列表框,在右边有一个棋盘。如何拖动一个项目,然后拖动到棋盘的一个或多个方格中。然后点击方块,这里会显示一些物品的信息。如果有人可以帮助我,我将不胜感激?谢谢大家。

【问题讨论】:

    标签: c# wpf


    【解决方案1】:

    【讨论】:

    • 谢谢,我认为我最大的问题是该项目如何容纳多个方格
    【解决方案2】:

    您好,这是一种让您朝着正确方向前进的方法

    http://johnnblade.wordpress.com/2012/06/12/drag-and-drop-grid-control-row-devexpress-wpf/

    如果您有更多问题,请告诉我

    【讨论】:

    • 我的示例是针对网格的,但您可以在其他控件上使用它
    • 谢谢,我认为我最大的问题是该项目如何能够容纳多个方格。我该怎么做?
    【解决方案3】:

    我已经完成了..但是在这里我将文件从桌面拖到我的列表框

     ` public MainPage()
        {
            InitializeComponent();
    
            CompositionTarget.Rendering +=new EventHandler(CompositionTarget_Rendering);
    
            FileBoard.Drop += new DragEventHandler(FileBoard_Drop);
        }
    

    `

    当你拖动元素时

     void FileBoard_Drop(object sender, DragEventArgs e)
        {
    
            if (e.Data != null)
            {
                FileInfo[] files = e.Data.GetData(DataFormats.FileDrop) as FileInfo[];
    
    
    
                foreach (FileInfo fi in files)
                {
                    _files.Enqueue(fi);
                }
    
            }
    
    
        }
    

    创建一个列表 DATAinGrid

    使用 CompositionTargetRendering 可以将文件从队列中取出

    private void CompositionTarget_Rendering(Object sender, EventArgs e)
        {
            if (_files.Count != 0)
            {
                // Create a photo
                FileInfo fi = _files.Dequeue();
    
             }
       }
    

    然后将itemsource分配给你的棋盘或棋盘项目...尝试修改我认为你会得到它的代码

    【讨论】:

      【解决方案4】:

      这是旧的,但我找到了一个 KISS 解决方案。

      使用文本块或网格中的图像创建您的棋盘网格。

      为要传递的数据创建一个模型。

      在 xml 中这样做:

      <TextBlock x:Name="myname" x:Uid="myname" Grid.Row="0" Grid.Column="1" Margin="3" Text="{Binding myfield}" Style="{DynamicResource myStyle}" AllowDrop="True" Drop="Square_Drop"/>
      <TextBlock x:Name="myname" x:Uid="myname" Grid.Row="1" Grid.Column="1" Margin="3" Text="{Binding myfield}" Style="{DynamicResource myStyle}" AllowDrop="True" Drop="Square_Drop"/>
      //etc etc etc
      
      <ListBox x:Name="myListBox" x:Uid="myListBox" 
                           ItemContainerStyle="{DynamicResource myListBoxListItemStyle}" Margin="10" 
                           DisplayMemberPath="myField" 
                           PreviewMouseLeftButtonDown="List_PreviewMouseLeftButtonDown">
      

      我强烈建议您使用资源来设置 TextBlock/Image 方块的样式。 (一白一黑!)

      Learn how here

      那么在你的c#后面你会需要:

      private void List_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
          {
              if (myListBox.SelectedItem != null)
              {
                  ListBox parent = (ListBox)sender;
                  myModel data = parent.SelectedItem as myModel;
      
                  if (data != null)
                  {
                      DragDrop.DoDragDrop(parent, data, DragDropEffects.Move);
                  }
              }
          }
      
          private void Square_Drop(object sender, DragEventArgs e)
          {
              MyModel data = e.Data.GetData(typeof(MyModel)) as MyModel;
      
              TextBlock tb = sender as TextBlock;
      
              tb.DataContext = data;
      
              //Add any database update code here
      
              refreshInterface();
          }
      

      【讨论】:

        猜你喜欢
        • 2011-12-08
        • 2011-12-23
        • 2015-03-25
        • 1970-01-01
        • 1970-01-01
        • 2021-01-08
        • 1970-01-01
        • 2012-02-28
        • 1970-01-01
        相关资源
        最近更新 更多