我正在编写一个自定义控件,我希望控件在用户点击控件时从编辑状态切换到正常状态.我正在处理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) 肯定有一种方便的方法:
我已经在我的主窗体上实现了鼠标拖动行为的"移动窗口",
我希望鼠标单击/移动事件被表单拦截,而不是由其中的控件拦截.
我想找到一个等效/复制鼠标事件的"KeyPreview"属性
此外,我想避免在12个控件的鼠标事件中单独将鼠标事件重定向到主窗体方法12次(这是我到目前为止找到的丑陋的解决方法)
有任何想法吗 ?