gpt4 book ai didi

wpf - 在 MouseUp WPF 上选择 ListBoxItem

转载 作者:行者123 更新时间:2023-12-04 18:19:27 24 4
gpt4 key购买 nike

我有一个多选的 ListBox。我正在其中进行拖放操作。我使用 Ctrl+A 选择所有项目。但是,一旦我单击一个项目开始拖动,项目就会被取消选择。有没有办法在鼠标上选择/取消选择列表框项。

最佳答案

ListBoxItem 覆盖它的 OnMouseLeftButtonDown 并调用处理选择的包含 ListBox 的方法。因此,如果您想将鼠标放在选定的列表框项上并启动拖动,则需要在 ListBoxItem 上发生这种情况之前开始拖动。所以你可以尝试处理 PreviewMouseLeftButtonDown 在 ListBox 上并检查 e.OriginalSource。如果那是 ListBoxItem 或列表框项中的元素(您需要沿着可视化树向上走),那么您可以启动拖动操作。例如。

private void OnPreviewLeftButtonDown(object sender, MouseButtonEventArgs e)
{
var source = e.OriginalSource as DependencyObject;

while (source is ContentElement)
source = LogicalTreeHelper.GetParent(source);

while (source != null && !(source is ListBoxItem))
source = VisualTreeHelper.GetParent(source);

var lbi = source as ListBoxItem;

if (lbi != null && lbi.IsSelected)
{
var lb = ItemsControl.ItemsControlFromItemContainer(lbi);
e.Handled = true;
DragDrop.DoDragDrop(....);
}

}

关于wpf - 在 MouseUp WPF 上选择 ListBoxItem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10988058/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com