【发布时间】:2023-03-11 15:11:01
【问题描述】:
在我的主视图中,我有一个Listbox,我为此设置了PreviewMouseLeftButtonDownEvent,我用它来支持拖放重新排序。
var style = ListBox.ItemContainerStyle;
style.Setters.Add(new Setter(AllowDropProperty, true));
style.Setters.Add(new EventSetter(PreviewMouseLeftButtonDownEvent,
new MouseButtonEventHandler(Input_Down)));
private void Input_Down(object sender, EventArgs e)
{
if (!(sender is ListBoxItem))
return;
var draggedItem = sender as ListBoxItem;
isDragging = true;
StartDrag(draggedItem);
}
private void StartDrag(ListBoxItem draggedItem)
{
draggedItem.IsSelected = true;
DragDrop.DoDragDrop(draggedItem, draggedItem.DataContext, DragDropEffects.Move);
}
在ListBox.ItemTemplate 中有一个带有更新命令的按钮:
<Button Command="{Binding Path=UpdateCommand}" Content="Button"/>
但是,当我设置PreviewMouseLeftButtonDownEvent 时,该命令从未被触发。如果我删除 PreviewMouseLeftButtonDownEvent 设置器,该命令可以正常工作。关于为什么会这样以及如何同时使用这两种方法的任何想法?
【问题讨论】:
-
您是否在您的
Input_Down方法中将e.Handled设置为true? -
没有,我基本上只是开始拖拽操作
-
你能贴出
Input_Down方法的代码吗? -
我更新了问题
-
问题是
DoDragDrop中断Click进程。试试<Button ... ClickMode="Press"/>是否有帮助,但这意味着点击会立即出现在按下而不是发布时
标签: c# wpf events listbox command