我跟着this tutorial来实现Drag & Drop在我的数据网格上。我根据this link更改了它,以便能够在组之间移动元素。MouseMove事件可以防止ComboBox被“打开”
我的datagrid包含一个按钮的列,所以我跟着this answer使按钮再次可用。我也有3列与组合框,他们不能使用(你可以点击它们,然后他们看起来像组合框,但第二次点击不会扩大它)。
其中两个定义为DataGridComboBoxColumn
,一个定义为DataGridTemplateColumn
和ComboBox
在标签DataGridTemplateColumn.CellEditingTemplate
中。
前两个是这样的:
<DataGridComboBoxColumn Header="Entity"
ItemsSource="{StaticResource tl}"
DisplayMemberPath="Name"
SelectedValuePath="Name"
SelectedValueBinding="{Binding Entity}"
x:Name="cmbEntity"></DataGridComboBoxColumn>
数据网格的定义是这样的:
<DataGrid Grid.Row="1" Name="myGrid" IsManipulationEnabled="True" ItemsSource="{Binding Source={StaticResource cvs}}" AutoGenerateColumns="False"
RowEditEnding="myGrid_RowEditEnding" PreviewKeyDown="myGrid_PreviewKeyDown" SelectedCellsChanged="myGrid_SelectedCellsChanged"
AllowDrop="True" MouseMove="DataGrid_MouseMove" PreviewMouseLeftButtonDown="DataGrid_PreviewMouseLeftButtonDown" Drop="DataGridView_Drop">
并且如上所述,该方法根据教程实现。我已经尝试在事件处理程序中使用e.Handled=false
,但它没有帮助(并且它可能无用,因为打开组合框不是事件?)
通过一次删除一个事件处理程序,我发现MouseMove事件的问题,代码如下:
void DataGrid_MouseMove(object sender, MouseEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed) {
Console.Out.WriteLine("MouseButtonState.Pressed");
DataGrid dataGrid = sender as DataGrid;
prevRowIndex = GetDataGridItemCurrentRowIndex(dataGrid, e.GetPosition);
if (prevRowIndex < 0) { return;}
dataGrid.SelectedIndex = prevRowIndex;
DefaultValue selectedDV = dataGrid.Items[prevRowIndex] as DefaultValue;
if (selectedDV == null) { return; }
DragDropEffects dragDropEffects = DragDropEffects.Move;
if (DragDrop.DoDragDrop(dataGrid, selectedDV, dragDropEffects) != DragDropEffects.None)
{
dataGrid.SelectedItem = selectedDV;
}
}
}
我不完全理解为什么发生这种情况,因为我真的不移动鼠标,我只要按一下含有组合的细胞框。是否有可能同时拥有,拖动&拖放和ComboBoxes?
编辑:我修改从the tutorial我用于显示我有问题的项目:Download from my dropbox 我改变了薪酬列是一个组合框(当然也添加了分组,因为我认为这可能是重要的)
如果你可以发布一个简单的项目来显示这个问题,那很好,否则你需要花费所有的教程来重现你的错误。 – netaholic
好吧,我会这样做,但这需要我一些时间(我无法上传完整的项目,因为它记录了我自己的代码)。 –
那么,它怎么样? – netaholic