Reputation: 12884
I already have a Custom Canvas class in which i have override the 'OnMoseDown' method. But now i also want to override 'Children.Add' method. But i didnt find any solution.
I want to get Child.ActualWidth when a child gets added to canvas.
Thanks.
Upvotes: 0
Views: 1736
Reputation: 12884
I just found my answer by myself. Hope this may help someone on some day.
Just added a Add method in my Custom class
public void Add(UserControl element)
{
bool alreadyExist = false;
element.Uid = element.Name;
foreach (UIElement child in this.Children)
{
if (child.Uid == element.Uid)
{
alreadyExist = true;
this.BringToFront(child);
}
}
if (!alreadyExist)
{
this.Children.Add(element);
this.UpdateLayout();
double top = (this.ActualHeight - element.ActualHeight) / 2;
double left = (this.ActualWidth - element.ActualWidth) / 2;
Canvas.SetLeft(element, left);
Canvas.SetTop(element, top);
}
}
Upvotes: 1
Reputation: 564851
This will likely not work correctly. A child will get added before it's ActualWidth
is set, as that property gets set during the layout phase.
Depending on your goals here, you may want to consider overriding MeasureOverride
and ArrangeOverride
instead, and making a custom layout panel instead of subclassing Canvas
.
Upvotes: 0