简体   繁体   English

如何使WPF Adorner可见

[英]How to make WPF Adorner visible

i'm struggling a bit about when or how to activate / make visible a MoveAdorner. 我在何时或如何激活/使MoveAdorner可见方面有些挣扎。

I try the following but have no success: In the element that is to be adorned i add the Adorner in the GotFocus event. 我尝试以下操作,但没有成功:在要装饰的元素中,我在GotFocus事件中添加了Adorner。 That alone did not suffice so i added a call to InvalidateVisual(). 单靠那还不够,所以我添加了对InvalidateVisual()的调用。 But nothing happens. 但是什么也没发生。 Has anyone a hint on how to make those Adorners Visible? 有没有人暗示如何使这些装饰物可见?

    protected void MyUIElement_GotFocus( object sender, RoutedEventArgs e )
    {
        AdornerLayer layer = AdornerLayer.GetAdornerLayer( this );
        layer.Add( new MoveAdorner( this ) );
        layer.InvalidateVisual( );
    }

For Clarification: the adorned element is a Control that is positioned inside a derived Panel of a custom ItemsControl. 为了澄清起见:装饰元素是一个控件,位于自定义ItemsControl的派生Panel内。

The MoveAdorner derives from Adorner and simply draws two Boxes on the top and bottom line of the control. MoveAdornerAdorner派生而来,仅在控件的顶部和底部绘制两个Box。

with kind regards 亲切的问候

In fact the Got/Lost Focus events are not very good for this situation. 实际上,“获得/丢失焦点”事件对于这种情况不是很好。 Imagine you want to show additional input controls that can get the focus. 想象一下,您想显示其他可以获得焦点的输入控件。

Now i've hooked up into the LeftButtonUpEvent and Hide all other Adorners and only Showing the Adorner of the current element. 现在,我已经连接到LeftButtonUpEvent并隐藏所有其他装饰器,并且仅显示当前元素的装饰器。

Also, the adorner is added on demand when requested. 同样,可根据要求添加装饰物。 This method is defined in the Control that is to be "adorned". 此方法在要“装饰”的控件中定义。

private void ShowAdorner( ) {
  Owner.HideAppointmentAdorners( );

  AdornerLayer layer = AdornerLayer.GetAdornerLayer( this );

  Adorner []adorners = layer.GetAdorners( this );

  if( adorners == null || adorners.Length == 0 )
  {
      layer.Add( new ResizingAdorner( this ) { Visibility = System.Windows.Visibility.Visible } );
  }
  else
  {
      for( int i = 0; i < adorners.Length; i++ )
      {
          adorners [ i ].Visibility = System.Windows.Visibility.Visible;
      }
  }

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM