是否可以给装饰器一个绝对位置或将它移动到例如更左边一点?
Is it possible to give an Adorner an absolute position or to move it for example a bit more to the left?
目前我遇到的问题是显示的错误遍及多个标签或文本框。我希望我能给装饰者一个绝对的位置。如果那不可能,我想将它移到左边。现在我无法在互联网上找到有关装饰物定位的有用信息。所以我想知道有没有办法改变装饰者的位置?
我可以将我的代码粘贴到此处,如果它有助于获得装饰器定位的解决方案。
我们将不胜感激
提前致谢
您应该派生自 Adorner 对象,并在那里覆盖位置确定逻辑,这将允许您执行各种技巧,包括绝对/相对定位。
public class ControlAdorner: Adorner
{
#region Private fields
// Utilized for caching of offset by x co-ordinate.
private double m_OffsetX = 0d;
// Uri private double m_OffsetY = 0d;
#endregion
#region Initialization
public ControlAdorner( UIElement adornedElement )
: base( adornedElement )
{
}
#endregion
#region Implementation
//Measures content.
protected override Size MeasureOverride( Size constraint )
{
m_child.Measure( constraint );
return AdornedElement.RenderSize;
}
//Arranges child control to the full size.
protected override Size ArrangeOverride( Size finalSize )
{
m_child.Arrange( new Rect( finalSize ) );
return m_child.RenderSize;
}
public override GeneralTransform GetDesiredTransform( GeneralTransform transform )
{
GeneralTransformGroup group = new GeneralTransformGroup();
group.Children.Add( transform );
group.Children.Add( new TranslateTransform( OffsetX, OffsetY ) );
return group;
}
//Get visual by index.
protected override Visual GetVisualChild( int index )
{
return m_child;
}
private static void OnOffsetXChanged( DependencyObject d, DependencyPropertyChangedEventArgs e )
{
ControlAdorner instance = (ControlAdorner)d;
instance.OnOffsetXChanged( e );
}
private void OnOffsetXChanged( DependencyPropertyChangedEventArgs e )
{
m_OffsetX = (double)e.NewValue;
if( OffsetXChanged != null )
{
OffsetXChanged( this, e );
}
}
private static void OnOffsetYChanged( DependencyObject d, DependencyPropertyChangedEventArgs e )
{
ControlAdorner instance = (ControlAdorner)d;
instance.OnOffsetYChanged( e );
}
private void OnOffsetYChanged( DependencyPropertyChangedEventArgs e )
{
m_OffsetY = (double)e.NewValue;
if( OffsetYChanged \!= null )
{
OffsetYChanged( this, e );
}
}
#endregion
#region Events
public event PropertyChangedCallback OffsetXChanged;
public event PropertyChangedCallback OffsetYChanged;
public double OffsetX
{
get
{
return m_OffsetX;
}
set
{
SetValue( OffsetXProperty, value );
}
}
public double OffsetY
{
get
{
return m_OffsetY;
}
set
{
SetValue( OffsetYProperty, value );
}
}
// Gets visual children count, always 1.
protected override int VisualChildrenCount
{
get
{
return 1;
}
}
#endregion
#region Dependency properties
public static readonly DependencyProperty OffsetXProperty = DependencyProperty.Register(
"OffsetX", typeof( double ), typeof( ControlAdorner ),
new FrameworkPropertyMetadata( 0d,
FrameworkPropertyMetadataOptions.AffectsArrange |
FrameworkPropertyMetadataOptions.AffectsParentArrange,
new PropertyChangedCallback( OnOffsetXChanged ) ) );
public static readonly DependencyProperty OffsetYProperty = DependencyProperty.Register(
"OffsetY", typeof( double ), typeof( ControlAdorner ),
new FrameworkPropertyMetadata( 0d,
FrameworkPropertyMetadataOptions.AffectsArrange |
FrameworkPropertyMetadataOptions.AffectsParentArrange,
new PropertyChangedCallback( OnOffsetYChanged ) ) );
#endregion
}
目前我遇到的问题是显示的错误遍及多个标签或文本框。我希望我能给装饰者一个绝对的位置。如果那不可能,我想将它移到左边。现在我无法在互联网上找到有关装饰物定位的有用信息。所以我想知道有没有办法改变装饰者的位置?
我可以将我的代码粘贴到此处,如果它有助于获得装饰器定位的解决方案。
我们将不胜感激
提前致谢
您应该派生自 Adorner 对象,并在那里覆盖位置确定逻辑,这将允许您执行各种技巧,包括绝对/相对定位。
public class ControlAdorner: Adorner
{
#region Private fields
// Utilized for caching of offset by x co-ordinate.
private double m_OffsetX = 0d;
// Uri private double m_OffsetY = 0d;
#endregion
#region Initialization
public ControlAdorner( UIElement adornedElement )
: base( adornedElement )
{
}
#endregion
#region Implementation
//Measures content.
protected override Size MeasureOverride( Size constraint )
{
m_child.Measure( constraint );
return AdornedElement.RenderSize;
}
//Arranges child control to the full size.
protected override Size ArrangeOverride( Size finalSize )
{
m_child.Arrange( new Rect( finalSize ) );
return m_child.RenderSize;
}
public override GeneralTransform GetDesiredTransform( GeneralTransform transform )
{
GeneralTransformGroup group = new GeneralTransformGroup();
group.Children.Add( transform );
group.Children.Add( new TranslateTransform( OffsetX, OffsetY ) );
return group;
}
//Get visual by index.
protected override Visual GetVisualChild( int index )
{
return m_child;
}
private static void OnOffsetXChanged( DependencyObject d, DependencyPropertyChangedEventArgs e )
{
ControlAdorner instance = (ControlAdorner)d;
instance.OnOffsetXChanged( e );
}
private void OnOffsetXChanged( DependencyPropertyChangedEventArgs e )
{
m_OffsetX = (double)e.NewValue;
if( OffsetXChanged != null )
{
OffsetXChanged( this, e );
}
}
private static void OnOffsetYChanged( DependencyObject d, DependencyPropertyChangedEventArgs e )
{
ControlAdorner instance = (ControlAdorner)d;
instance.OnOffsetYChanged( e );
}
private void OnOffsetYChanged( DependencyPropertyChangedEventArgs e )
{
m_OffsetY = (double)e.NewValue;
if( OffsetYChanged \!= null )
{
OffsetYChanged( this, e );
}
}
#endregion
#region Events
public event PropertyChangedCallback OffsetXChanged;
public event PropertyChangedCallback OffsetYChanged;
public double OffsetX
{
get
{
return m_OffsetX;
}
set
{
SetValue( OffsetXProperty, value );
}
}
public double OffsetY
{
get
{
return m_OffsetY;
}
set
{
SetValue( OffsetYProperty, value );
}
}
// Gets visual children count, always 1.
protected override int VisualChildrenCount
{
get
{
return 1;
}
}
#endregion
#region Dependency properties
public static readonly DependencyProperty OffsetXProperty = DependencyProperty.Register(
"OffsetX", typeof( double ), typeof( ControlAdorner ),
new FrameworkPropertyMetadata( 0d,
FrameworkPropertyMetadataOptions.AffectsArrange |
FrameworkPropertyMetadataOptions.AffectsParentArrange,
new PropertyChangedCallback( OnOffsetXChanged ) ) );
public static readonly DependencyProperty OffsetYProperty = DependencyProperty.Register(
"OffsetY", typeof( double ), typeof( ControlAdorner ),
new FrameworkPropertyMetadata( 0d,
FrameworkPropertyMetadataOptions.AffectsArrange |
FrameworkPropertyMetadataOptions.AffectsParentArrange,
new PropertyChangedCallback( OnOffsetYChanged ) ) );
#endregion
}