wpf DataGrid Scroll with mouseclick scrollBar Error when Row Drag and Drop
wpf DataGrid Scroll with mouseclick scrollBar Error when Row Drag and Drop
我在 DataGrid 中写了 DoDragDrop。
但是,由于出现拖动错误,我无法在 DataGrid 上单击和拖动滚动条。
但是我可以用鼠标滚轮滚动。
我该如何解决?这是我的代码的一部分。
.cs文件
private void datagrid_MouseMove(object sender, MouseEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed)
{
Point currentPosition = e.GetPosition(incidentList);
object selectedItem = datagrid.SelectedItem;
if (selectedItem != null)
{
DataGridRow container = (DataGridRow)datagrid.ItemContainerGenerator.ContainerFromItem(selectedItem);
var dataObj = new DataObject();
dataObj.SetData("DragSource", container);
if (container != null)
{
DragDrop.DoDragDrop(container, dataObj, DragDropEffects.Copy);
}
}
}
}
.xaml 文件
<DataGrid x:Name="datagrid" ColumnHeaderStyle="{StaticResource MyColumnHeader}"
Style="{DynamicResource DataGridStyle}" CanUserAddRows="False" VerticalAlignment="Stretch"
Height="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Grid}}, Path=ActualHeight}"
HorizontalAlignment="Stretch" MinHeight="150" SelectionMode="Single"
ItemsSource="{Binding myListData, UpdateSourceTrigger=PropertyChanged}" AutoGenerateColumns="False"
SelectedItem="{Binding SelectMyRow}"
MouseMove="datagrid_MouseMove"> .....
在行而不是数据网格上注册您的处理程序:
<DataGrid>
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<EventSetter Event="MouseMove" Handler="RowMoveHandler"/>
</Style>
</DataGrid.RowStyle>
然后将发件人行作为容器:
private void RowMoveHandler(object sender, MouseEventArgs e)
{
var container = sender as DataGridRow;
if (container != null && e.LeftButton == MouseButtonState.Pressed)
{
var dataObj = new DataObject();
dataObj.SetData("DragSource", container);
DragDrop.DoDragDrop(container,
dataObj,
DragDropEffects.Copy);
}
}
我在 DataGrid 中写了 DoDragDrop。
但是,由于出现拖动错误,我无法在 DataGrid 上单击和拖动滚动条。
但是我可以用鼠标滚轮滚动。
我该如何解决?这是我的代码的一部分。
.cs文件
private void datagrid_MouseMove(object sender, MouseEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed)
{
Point currentPosition = e.GetPosition(incidentList);
object selectedItem = datagrid.SelectedItem;
if (selectedItem != null)
{
DataGridRow container = (DataGridRow)datagrid.ItemContainerGenerator.ContainerFromItem(selectedItem);
var dataObj = new DataObject();
dataObj.SetData("DragSource", container);
if (container != null)
{
DragDrop.DoDragDrop(container, dataObj, DragDropEffects.Copy);
}
}
}
}
.xaml 文件
<DataGrid x:Name="datagrid" ColumnHeaderStyle="{StaticResource MyColumnHeader}"
Style="{DynamicResource DataGridStyle}" CanUserAddRows="False" VerticalAlignment="Stretch"
Height="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Grid}}, Path=ActualHeight}"
HorizontalAlignment="Stretch" MinHeight="150" SelectionMode="Single"
ItemsSource="{Binding myListData, UpdateSourceTrigger=PropertyChanged}" AutoGenerateColumns="False"
SelectedItem="{Binding SelectMyRow}"
MouseMove="datagrid_MouseMove"> .....
在行而不是数据网格上注册您的处理程序:
<DataGrid>
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<EventSetter Event="MouseMove" Handler="RowMoveHandler"/>
</Style>
</DataGrid.RowStyle>
然后将发件人行作为容器:
private void RowMoveHandler(object sender, MouseEventArgs e)
{
var container = sender as DataGridRow;
if (container != null && e.LeftButton == MouseButtonState.Pressed)
{
var dataObj = new DataObject();
dataObj.SetData("DragSource", container);
DragDrop.DoDragDrop(container,
dataObj,
DragDropEffects.Copy);
}
}