C# 如何将项目内容从WPF comboBox拖放到WPF TextBox
我使用以下WPF代码C# 如何将项目内容从WPF comboBox拖放到WPF TextBox,c#,.net,wpf,drag-and-drop,C#,.net,Wpf,Drag And Drop,我使用以下WPF代码 <ComboBox Name="cmbFunctionsList" Grid.Row="3" Grid.Column="1" DropDownClosed="OnCmbFunctionsListDropDownClosed" DisplayMemberPath="FunctionItem" SelectedValuePath="FunctionValue"
<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;
}
}
私有void onFunctionsListReviewMouseLeftButtonDown(对象发送器,鼠标按钮ventargs e)
{
//存储鼠标位置
_startPoint=e.GetPosition(null);
}
私有void OnFunctionsListPreviewMouseMove(对象发送方,MouseEventArgs e)
{
//拖放代码被注释
//获取当前鼠标位置
鼠标点=e.GetPosition(null);
向量差=_起始点-鼠标点;
如果(e.LeftButton==鼠标按钮状态。按下&&
Math.Abs(diff.X)>系统参数。最小水平牵引距离||
数学。Abs(差异Y)>系统参数。最小垂直牵引距离)
{
组合框cmb=发送方作为组合框;
cmb.StaysOpenOnEdit=true;
ComboBoxItem cmbItem=Findanchester((DependencyObject)e.OriginalSource);
如果(cmbItem!=null)
{
如果(CMB FunctionsList.SelectedIndex>-1&&CMB FunctionsList.IsDropDownOpen==true)
{
//查找ComboBoxItem后面的数据
DataRowView dr=(DataRowView)cmb.ItemContainerGenerator.ItemFromContainer(cmbItem);
字符串draggedText=(字符串)dr[1];
//初始化拖放操作
DataObject dragData=新数据对象(“stringFormat”,DragedText);
DragDrop.DoDragDrop(cmbItem、dragData、DragDropEffects.Copy);
}
}
}
}
私有void OnTxtExpressionDragEnter(对象发送方,DragEventArgs e)
{
如果(!e.Data.GetDataPresent(“stringFormat”)| |发送方==e.Source)
{
e、 效果=DragDropEffects.Copy;
}
}
私有void OnTxtExpressionDrop(对象发送方,DragEventArgs e)
{
if(例如Data.GetDataPresent(“stringFormat”))
{
String droppedText=e.Data.GetData(“stringFormat”)作为字符串;
TextBox txtExp=发送方作为TextBox;
Text=droppedText;
}
}
但拖放功能仍然不起作用。此外,当我试图从组合框中拖动项目时,它会自动关闭
您能告诉我这里缺少什么吗?Drop事件在WPf中自动处理,因此无需写入事件删除或预览删除Drop事件在WPf中自动处理,因此无需写入事件删除或预览删除FindAncestor与相对资源绑定一起使用。我在这里没有看到任何RelativeSource绑定。此链接可能会有所帮助:FindAncestor与RelativeSource绑定一起使用。我看不到任何相关资源绑定正在这里进行。此链接可能有帮助: