繁体   English   中英

如何将项目的内容从WPF comboBox拖放到WPF TextBox

[英]How to Drag and drop the content of an Item from WPF comboBox to WPF TextBox

我正在使用以下WPF代码

<ComboBox Name="cmbFunctionsList"  Grid.Row="3" Grid.Column="1"  
                      DropDownClosed="OnCmbFunctionsListDropDownClosed"
                      DisplayMemberPath="FunctionItem" SelectedValuePath="FunctionValue" 
                      PreviewMouseLeftButtonDown="OnFunctionsListPreviewMouseLeftButtonDown"
                      PreviewMouseMove="OnFunctionsListPreviewMouseMove"                      
                      MinHeight="25"  Margin="2,2,2,0" VerticalAlignment="Center"/>

<TextBox Grid.Column="1" Margin="2" Grid.Row="2" Grid.ColumnSpan="2" Name="txtExpression"
            AllowDrop="True" Drop="OnTxtExpressionDrop" DragEnter="OnTxtExpressionDragEnter" />





 private void OnFunctionsListPreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        //storing the mouse position
        _startPoint = e.GetPosition(null);
    }
private void OnFunctionsListPreviewMouseMove(object sender, MouseEventArgs e)
{

    // Drag and Drop Code is commented
    // Get the current mouse position
    Point mousePos = e.GetPosition(null);
    Vector diff = _startPoint - mousePos;

    if (e.LeftButton == MouseButtonState.Pressed &&
       Math.Abs(diff.X) > SystemParameters.MinimumHorizontalDragDistance ||
       Math.Abs(diff.Y) > SystemParameters.MinimumVerticalDragDistance)
    {
        ComboBox cmb = sender as ComboBox;
        cmb.StaysOpenOnEdit = true;

        ComboBoxItem cmbItem = FindAnchestor<ComboBoxItem>((DependencyObject)e.OriginalSource);

        if (cmbItem != null)
        {
            if (cmbFunctionsList.SelectedIndex > -1 && cmbFunctionsList.IsDropDownOpen == true)
            {
                // Find the data behind the ComboBoxItem
                DataRowView dr = (DataRowView)cmb.ItemContainerGenerator.ItemFromContainer(cmbItem);

                string draggedText = (String)dr[1];

                // Initialize the drag & drop operation
                DataObject dragData = new DataObject("stringFormat", draggedText);
                DragDrop.DoDragDrop(cmbItem, dragData, DragDropEffects.Copy);
            } 
        }
    }
}


 private void OnTxtExpressionDragEnter(object sender, DragEventArgs e)
        {

            if (!e.Data.GetDataPresent("stringFormat") || sender == e.Source)
            {
                e.Effects = DragDropEffects.Copy;
            }

        }

  private void OnTxtExpressionDrop(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent("stringFormat"))
            {
                String droppedText = e.Data.GetData("stringFormat") as String;
                TextBox txtExp = sender as TextBox;
                txtExp.Text = droppedText;
            }

        }

但是拖放功能仍然无法使用。 而且,当我尝试从comboBox拖动项目时,它会自动关闭。

你能告诉我我在这里缺少什么吗?

Drop事件在WPf中自动处理,因此无需编写事件Drop或Preview Drop

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM