相关疑难解决方法(0)

控件如何在该控件外部处理鼠标单击?

我正在编写一个自定义控件,我希望控件在用户点击控件时从编辑状态切换到正常状态.我正在处理LostFocus事件,这有助于当用户选中时或者他们点击另一个可聚焦的控件时.但如果他们没有点击Focusable,它就不会切换出它的编辑状态.所以我有两个解决方案:

  • 当树进入编辑状态并为其添加处理程序MouseDownEvent(并处理"已处理"事件)时,将树向上移动到最顶层元素.在处理程序中,我将控件从其编辑状态中踢出,并从最顶层的元素中删除处理程序.这看起来有点像黑客,但它可能会运作良好.

示例代码:

private void RegisterTopMostParentMouseClickEvent()
{
   _topMostParent = this.FindLastVisualAncestor<FrameworkElement>();
   if ( _topMostParent == null )
      return;
   _topMostParent.AddHandler( Mouse.MouseDownEvent, new MouseButtonEventHandler( CustomControlMouseDownEvent ), true );
}

private void UnRegisterTopMostParentMouseClickEvent()
{
   if ( _topMostParent == null )
      return;
   _topMostParent.RemoveHandler( Mouse.MouseDownEvent, new MouseButtonEventHandler( CustomControlMouseDownEvent ) );
   _topMostParent = null;
}
Run Code Online (Sandbox Code Playgroud)
  • 使用Mouse.PreviewMouseDownOutsideCapturedElement并向我的控件添加处理程序.在处理程序中,我将控件从其编辑状态中踢出.但我似乎没有让事件发生.Mouse.PreviewMouseDownOutsideCapturedElement什么时候开始?

示例代码:

AddHandler( Mouse.PreviewMouseDownOutsideCapturedElementEvent, new MouseButtonEventHandler( EditableTextBlockPreviewMouseDownOutsideCapturedElementEvent ), true );
Run Code Online (Sandbox Code Playgroud)

c# wpf custom-controls mouseevent

25
推荐指数
2
解决办法
2万
查看次数

Winforms:首先在主窗体上拦截鼠标事件,而不是在控件上

肯定有一种方便的方法:

我已经在我的主窗体上实现了鼠标拖动行为的"移动窗口",
我希望鼠标单击/移动事件被表单拦截,而不是由其中的控件拦截.

我想找到一个等效/复制鼠标事件的"KeyPreview"属性

此外,我想避免在12个控件的鼠标事件中单独将鼠标事件重定向到主窗体方法12次(这是我到目前为止找到的丑陋的解决方法)

有任何想法吗 ?

c# winforms

8
推荐指数
2
解决办法
1万
查看次数

标签 统计

c# ×2

custom-controls ×1

mouseevent ×1

winforms ×1

wpf ×1