Yes I have tried this. Look at this little program:
class Program
{
[STAThread]
static void Main(string[] args)
{
Window w = new Window();
StackPanel p = new StackPanel();
w.Content = p;
w.SizeToContent = SizeToContent.WidthAndHeight;
p.Children.Add(new CustomControl());
for (int i = 0; i< 4; i++)
{
Button b = new Button();
b.Content = "Button " + i.ToString();
b.Margin = new Thickness(10);
p.Children.Add(b);
}
Application app = new Application();
app.Run(w);
}
}
public class CustomControl : FrameworkElement
{
public CustomControl()
{
this.ClipToBounds = false;
}
protected override Size MeasureOverride(Size availableSize)
{
return Size.Empty;
}
protected override void OnRender(DrawingContext
drawingContext)
{
foreach (DependencyObject depObj in
LogicalTreeHelper.GetChildren(this.Parent))
{
FrameworkElement f = depObj as FrameworkElement;
if (f != null)
{
if (f == this)
{
continue;
}
GeneralTransform transform =
f.TransformToVisual(this);
Point point = transform.Transform(new Point(0,
0));
Console.WriteLine(f.ToString() + " " +
point.ToString());
drawingContext.DrawLine(new Pen(Brushes.Red,2),new
Point(0,0),new Point(point.X + f.ActualWidth / 2,point.Y));
}
}
drawingContext.DrawRectangle(Brushes.LightBlue,(Pen)null,
new Rect(RenderSize));
}
}
I would like to draw lines to each Button. Unfortuantely at the time
the OnRender of CustomControl is called all the other Buttons are not
yet rendered or something and therefore their offset always returns
0,0. If the CustomControl is added last (after all the other butttons)
the offset is returned correctly. But I can't rely on the user of my
control to add it at last, so this is not an option.
So I need to find a better time to walk the the tree, store the
coordinates there and use them to draw the lines in the Onrender.The
question now, is what is the preferred override to walk the tree and
where I can be sure that the offsets are returned correctly?
On 18 Mai, 03:50, "Bill Henning" <no_s...@hotmail.com> wrote:
> Hi Robert,
>
> All you have to do is get a GeneralTransform from one Visual to the other like this:
>
> GeneralTransform transform = this.DialogLauncherButton.TransformToAncestor(this);
> Then once you have that GeneralTransform, you can call GeneralTransform.Transform on that instance to convert a Point from one coordinate system to the other.
> --
>
> Bill Henning
> Actipro Software
>
> WPF Ribbon and Wizard Controls -http://www.actiprosoftware.com/Products/#WPF
>
>
>
> "Robert Ludig" <schwertfischtromb...@gmx.de> wrote in messagenews:1179440453.581479.241290@y80g2000hsf.googlegroups.com...
> >I have a custom control that lives in a Panel together with other
> > controls. Now that custom control needs to draw some lines to those
> > other controls. How can it find the position (coordinates) of them
> > relative to its own coordinate system. So that in the OnRender I could
> > do:
>
> > drawingContext.DrawLine(new Pen(),new
> > Point(0,0),relativeLocationOfOtherElement);- Zitierten Text ausblenden -
>
> - Zitierten Text anzeigen -