Reputation: 46
I found some kind of a strange behavior when I use this method :
private Point GetPosition(Visual element)
{
GeneralTransform positionTransform = element.TransformToAncestor(this);
Point areaPosition = positionTransform.Transform(new Point(0, 0));
return areaPosition;
}
I called this method at the end of the CTOR of my user control's code behind :
public Agenda()
{
InitializeComponent();
AgendaHelper.StyleGrid(GridAppointment, this.FindResource("btnStyle") as Style, 0);
AgendaScrolling.ScrollToVerticalOffset(YSCROLL_OFFSET);
timeSlot = 1200.0 / (24.0 * 4.0);
isRectDragging = false;
isRectResize = false;
isMouseDown = false;
TickNumber = 0;
Point test = GetPosition(AgendaScrolling);}
and this error arises :
We see here that AgendaScrolling has all default values which is very surprising. What is very confusing happens if I call the same method in response of MouseMove event where everything work fine.
My question is : Why AgendaScrolling has all his properties set to default in CTOR after the call of InitializeComponent and, if it's normal behavior, why these properties doesn't remain at this state after that? (I mention that xaml code of AgendaScrolling contains some UIElements, I also use MVVM pattern but it does not seems to be the origin of my confusion).
EDIT :
Here is the exception message :
"The specified Visual object is not an ancestor of this Visual object"
Upvotes: 0
Views: 62
Reputation: 169360
this
, i.e. the UserControl
, cannot possibly be a visual ancestor before it has been added to the visual tree and the framework cannot add it to the tree before it has created an instance of it.
Move your call to GetPosition
to a Loaded
event handler:
public Agenda()
{
InitializeComponent();
...
Loaded += (s, e) =>
{
Point test = GetPosition(AgendaScrolling);
};
}
Upvotes: 2