WPF列表视图修改

发布于 2024-08-17 15:38:42 字数 109 浏览 5 评论 0原文

出于一个非常具体的原因,我想在鼠标按钮松开时选择 ListViewItems,而不是实际上在按下鼠标按钮时选择。我希望将此行为嵌入到控件中。有可能实现这一目标吗?谁能给出提示吗?

For a very specific reason I want to select ListViewItems on mouse button up, not actually on mouse button down. I want this behaviour to be embedded in the control. Is it possible to achieve this? can anyone give hint?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不气馁 2024-08-24 15:38:42

是的,使用附加属性绝对是可能的。定义一个名为 SelectOnMouseUp 的附加属性,当它设置为 true 时,挂钩到 ItemsContainerGenerator 事件以发现何时添加新的项目容器。然后,当您收到新项目容器的事件时,挂钩其 PreviewMouseDown 并忽略它(将 e.Handled 设置为 true),然后挂钩其 MouseUp 事件并执行选择(将 IsSelected 设置为 true)。

Yes it's definitely possible using attached properties. Define an attached property called SelectOnMouseUp and when it's set to true, hook to your ItemsContainerGenerator events to discover when a new item container is added. Then when you get an event for a new item container, hook into its PreviewMouseDown and ignore it (set e.Handled to true), and hook into its MouseUp event and perform the selection (set IsSelected to true).

蓝咒 2024-08-24 15:38:42

Aviad P. 的答案是一个很好的答案,并且巧妙地使用了附加属性,但我大多数时候倾向于使用不同的技术:

  1. 子类 ListViewItem。
  2. 重写 OnMouseLeftButtonDown 和 OnMouseRightButton 不执行任何操作。
  3. 重写 OnMouseLeftButtonUp / OnMouseRightButtonUp 以调用 base.OnMouseLeftButtonDown / base.OnMouseRightButtonDown。
  4. 子类列表视图。
  5. 重写 GetContainerForItemOverride() 以返回 ListViewItem 重写

这对我来说似乎比订阅 ItemContainer 事件并动态添加处理程序更容易。

它是这样的:

public class MouseUpListViewItem : ListViewItem
{
  protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e) {}
  protected override void OnMouseRightButtonDown(MouseButtonEventArgs e) {}

  protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
  {
    base.OnMouseLeftButtonDown(e);
  }
  protected override void OnMouseRightButtonUp(MouseButtonEventArgs e)
  {
    base.OnMouseRightButtonDown(e);
  }
}
public class MouseUpListView : ListView
{
  protected override DependencyObject GetContainerForItemOverride()
  {
    return new MouseUpListViewItem();
  }
}

我喜欢这种技术,因为涉及的代码较少。

Aviad P.'s answer is a good one and a clever use of attached properties, but I tend to use a different technique most of the time:

  1. Subclass ListViewItem.
  2. Override OnMouseLeftButtonDown and OnMouseRightButton to do nothing.
  3. Override OnMouseLeftButtonUp / OnMouseRightButtonUp to call base.OnMouseLeftButtonDown / base.OnMouseRightButtonDown.
  4. Subclass ListView.
  5. Override GetContainerForItemOverride() to return your ListViewItem override

This seems easier to me than subscribing to ItemContainer events and adding handlers dynamically.

This is what it looks like:

public class MouseUpListViewItem : ListViewItem
{
  protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e) {}
  protected override void OnMouseRightButtonDown(MouseButtonEventArgs e) {}

  protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
  {
    base.OnMouseLeftButtonDown(e);
  }
  protected override void OnMouseRightButtonUp(MouseButtonEventArgs e)
  {
    base.OnMouseRightButtonDown(e);
  }
}
public class MouseUpListView : ListView
{
  protected override DependencyObject GetContainerForItemOverride()
  {
    return new MouseUpListViewItem();
  }
}

I like this technique because there is less code involved.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文