This is part 2 of a series you may want to read part 1 first.
In the previous post in this series we created a trivial adorner and placed it over a button – in this post we will look at placing something more complex inside an adorner.
We’ve already seen Adroner is a FrameworkElement and we know FrameworkElement can contain other visuals inside it – so let’s just write an adorner that contains a control:
using System;
using System.Windows.Documents;
using System.Windows.Controls;
using System.Windows;
using System.Windows.Media;
namespace AdornerDemo
{
class ControlAdorner : Adorner
{
private Control _child;
public ControlAdorner(UIElement adornedElement)
: base(adornedElement)
{
}
protected override int VisualChildrenCount
{
get
{
return 1;
}
}
protected override Visual GetVisualChild(int index)
{
if (index != 0) throw new ArgumentOutOfRangeException();
return _child;
}
public Control Child
{
get { return _child; }
set
{
if (_child != null)
{
RemoveVisualChild(_child);
}
_child = value;
if (_child != null)
{
AddVisualChild(_child);
}
}
}
protected override Size MeasureOverride(Size constraint)
{
_child.Measure(constraint);
return _child.DesiredSize;
}
protected override Size ArrangeOverride(Size finalSize)
{
_child.Arrange(new Rect(new Point(0, 0), finalSize));
return new Size(_child.ActualWidth, _child.ActualHeight);
}
}
}
This is a completely standard implementation of a FramwrorkElement that contains a child control.
You may be asking yourself why a control and not a Visual or a UIElement, this is because the way we will use this adorner in a future post.
Now let’s use our new adorner by placing a button over the button from our previous program:
private void Window_Loaded(object sender, RoutedEventArgs e)
{
Button inAdorner = new Button()
{
HorizontalAlignment = HorizontalAlignment.Right,
Content = "X",
};
ControlAdorner adorner = new ControlAdorner(Btn)
{
Child = inAdorner,
};
AdornerLayer.GetAdornerLayer(Btn).Add(adorner);
}
Yes, this is a silly example, but think what you can do by placing a UserControl over an existing control.
In the next post we will look into the potential of the adorner layer.
posted @ Monday, June 28, 2010 11:48 AM