简体   繁体   中英

WPF and C# - Issue with GeneralTransform and UIElement.TransformToVisual

I've only been working with WPF for a few months, but I have an extensive WinForm, ASP.NET, and Flex background. I am attempting to draw a user control which looks like the following IMAGE .

We are using the MVVM pattern. When the user control first loads, everything draws correctly. The control consists of a canvas. Within the canvas exists a radial panel which will geometrically place UIElements which are added. The circular nodes are drawn into the radial panel. The PolyLines shown are drawn in the canvas. I get the end points of the PolyLine by using a GeneralTransform and a call to UIElement.TransformToVisual. Where the UIElement in the call is the blue node in the radial panel and it is transforming with the radial panel. When the control is loaded the first time, everything does draw correctly. Where it fails is when the MVVM pattern informs there is an update to the drawing.

When I attempt to refresh based off the system update, I have confirmed all of my collections contain the proper data. So the MVVM pattern is behaving like it should. However, when I attempt to draw the lines, my call to TransformToVisual is returning a point of 0,0 instead of the value it does during the load. This causes my polylines to draw at the top left of the control instead of connecting the two nodes.

Here is the order of operations:

1) User Control load will build my collections used to draw the control. These collections are built local to the User Control and the data comes from a master collection of info which resides in the view model. The user control registers a refresh method in itself to a UI refresh broadcast message.

2) The user attempts to connect two blue nodes through a wizard. The connector is saved and the refresh UI message is broadcast.

3) The User Control invokes the Refresh method. Here I am looping the children of the canvas and removing the PolyLines. I am also calling radialPanel.Children.Clear. I then perform the same code which built my collections at load. I then invalidate my user control to invoke the OnRender. The OnRender will then attempt to draw the Polylines to connect the nodes. The nodes are always placed properly in the radial panel.

4) The line connecting action is called from the OnRender. Within it I am using the GeneralTransform gt = node.TransformToVisual(radialPanel). This call always returns 0,0 when the user control is "refreshed" but always works the first load for the control.

I'm fairly positive the issue lies with how I refresh the user control based on the network notification. I've tried to clear the controls, then re-do the same load operations, but still no luck. I've executed InvalidateVisual against the user control and tried to do everything in my overriden OnRender still no luck; 0,0 is still returned.

Does anyone have any ideas on what could be happening to the GeneralTransform? I'm at a dead end and any new path would help.

I figured it out. So I'm posting the answer in case anyone else deals with this issue.

The problem was I wasn't forcing MeasureOverride and ArrangeOverride of the radial panel before attempting to draw the lines. Without these two called, the nodes are at position 0,0 at the time I connect my lines. By forcing them to call before drawing the lines, the TransformToVisual works properly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM