Reputation:
The following method thrwos an exception in the line:
Point childPosition = vb.TransformToAncestor(surfaceWindow).Transform(new Point(0, 0));
But if you look at the code, the vb is surely a child of surfaceWindow. So why isn't this working?
if (!isExpanded())
{
Viewbox vb = new Viewbox();
ClassMetricView metricView = new ClassMetricView();
metricView.Width = 300;
metricView.Height = 300;
metricView.ClassName = this.name;
metricView.NumberOfMetrics = 6;
metricView.LOC = this.getLoc();
metricView.FanIn = this.getFanIn();
metricView.FanOut = this.getFanOut();
metricView.buildComponent();
vb.Child = metricView;
vb.AddHandler(StackPanel.SizeChangedEvent, new System.Windows.SizeChangedEventHandler(SizeChangedHandler));
surfaceWindow.ClassScatter.Items.Add(vb);
this.setExpanded(true);
//Create line to connect these UI elements
Point parentPosition = surfaceWindow.RootContainer.TransformToAncestor(surfaceWindow).Transform(new Point(0, 0));
Point childPosition = vb.TransformToAncestor(surfaceWindow).Transform(new Point(0, 0));
Line line = new Line();
line.X1 = parentPosition.X;
line.Y1 = parentPosition.Y;
line.X2 = childPosition.X;
line.Y2 = childPosition.Y;
line.Stroke = System.Windows.Media.Brushes.Black;
line.StrokeThickness = 2;
surfaceWindow.RootGrid.Children.Add(line);
}
EDIT: I found maybe an answer to my problem:
Error when using TransformToAncestor: "The specified Visual is not an ancestor of this Visual."
the problem is, I don't understand the solution. Can anyone explain?
EDIT 2: I tried to implement this dispatcher. But still the same exception is thrown. Any hints would be really great!
public void expand(SurfaceWindow1 surfaceWindow)
{
_surfaceWindow = surfaceWindow;
Logging.Logger.getInstance().log("Expand class " + name);
if (!isExpanded())
{
Viewbox vb = new Viewbox();
ClassMetricView metricView = new ClassMetricView();
metricView.Width = 300;
metricView.Height = 300;
metricView.ClassName = this.name;
metricView.NumberOfMetrics = 5;
metricView.NumberOfRevisions = 6;
metricView.MetricsName = new string[] { "LOC", "FanIn", "FanOut", "NOM", "McCabe"};
int[,] values = { { 10, 10, 10, 10, 10}, {20, 20, 20, 20, 20}, {30, 30, 30, 30, 30}, {40, 40, 40, 40, 40}, {50, 50, 50, 50, 50}, {60, 60, 60, 60, 60} };
metricView.Metrics = values;
metricView.buildComponent();
vb.Child = metricView;
vb.AddHandler(StackPanel.SizeChangedEvent, new System.Windows.SizeChangedEventHandler(SizeChangedHandler));
surfaceWindow.ClassScatter.Items.Add(vb);
this.setExpanded(true);
//Create line to connect these UI elements
System.Threading.Thread thread = new System.Threading.Thread(
new System.Threading.ThreadStart(
delegate()
{
vb.Dispatcher.Invoke(
System.Windows.Threading.DispatcherPriority.Normal,
new Action(
delegate()
{
SetStatus(vb);
}
));
}
));
thread.Start();
}
}
private void SetStatus(Viewbox vb)
{
Point parentPosition = _surfaceWindow.RootContainer.TransformToAncestor(_surfaceWindow).Transform(new Point(0, 0));
Point childPosition = vb.TransformToAncestor(Window.GetWindow(vb)).Transform(new Point(0, 0));
//Point childPosition = new Point(0, 0);
Line line = new Line();
line.X1 = parentPosition.X;
line.Y1 = parentPosition.Y;
line.X2 = childPosition.X;
line.Y2 = childPosition.Y;
line.Stroke = System.Windows.Media.Brushes.Black;
line.StrokeThickness = 2;
_surfaceWindow.RootGrid.Children.Add(line);
Console.WriteLine("Draw line with position: " + line.X1 + "/" + line.Y1 + "/" + line.X2 + "/" + line.Y2);
}
Upvotes: 2
Views: 8140
Reputation: 7
I encountered the same error while assigning a list of items to a DataGrid. Turned out that if the list was empty, even though the list wasn't null.
To solve that, adding a validation is enough:
if (listOfObjectsToShowInTheGrid?.Any() ?? false)
{
this.DataGrid1.ItemsSource = listOfObjectsToShowInTheGrid;
this.DataGrid1.UpdateLayout();
}
else
{
// Do somethig if the list is empty
}
Upvotes: -2
Reputation: 52229
I was finally able to solve the problem. It seems true that the ScatterViewItem is not yet added to the VisualTree right after the call
surfaceWindow.ClassScatter.Item.Add(vb);
So I added a SizeChangedEventHandler and a ScatterManipulationDeltaEventHandler to the ScatterViewItem and add the lines there.
Thanks to Bart, who helped me with this solution
if (!isExpanded())
{
Viewbox vb = new Viewbox();
ClassMetricView metricView = new ClassMetricView();
metricView.Width = 300;
metricView.Height = 300;
metricView.ClassName = this.name;
metricView.NumberOfMetrics = 5;
metricView.NumberOfRevisions = 6;
metricView.MetricsName = new string[] { "LOC", "FanIn", "FanOut", "NOM", "McCabe" };
int[,] values = { { 10, 10, 10, 10, 10 }, { 20, 20, 20, 20, 20 }, { 30, 30, 30, 30, 30 }, { 40, 40, 40, 40, 40 }, { 50, 50, 50, 50, 50 }, { 60, 60, 60, 60, 60 } };
metricView.Metrics = values;
metricView.buildComponent();
vb.Child = metricView;
vb.AddHandler(StackPanel.SizeChangedEvent, new System.Windows.SizeChangedEventHandler(SizeChangedHandler));
surfaceWindow.ClassScatter.Items.Add(vb);
ScatterViewItem svItem = _surfaceWindow.ClassScatter.ItemContainerGenerator.ContainerFromItem(vb) as ScatterViewItem;
svItem.Tag = this.name;
svItem.AddHandler(ScatterViewItem.ScatterManipulationDeltaEvent, new ScatterManipulationDeltaEventHandler(MovementHandler));
svItem.AddHandler(StackPanel.SizeChangedEvent, new System.Windows.SizeChangedEventHandler(ScatterSizeChanged));
this.setExpanded(true);
}
and
public void MovementHandler(object sender, ScatterManipulationDeltaEventArgs e)
{
updateConnectingLines(sender);
}
public void ScatterSizeChanged(object sender, SizeChangedEventArgs e)
{
updateConnectingLines(sender);
}
private void updateConnectingLines(object sender)
{
removeOldLines((sender as ScatterViewItem).Tag as String);
Point childPosition = (sender as ScatterViewItem).TransformToAncestor(_surfaceWindow.ClassScatter).Transform(new Point(0, 0));
Point parentPosition = _surfaceWindow.RootContainer.TransformToAncestor(_surfaceWindow).Transform(new Point(0, 0));
Line line = new Line();
line.X1 = parentPosition.X;
line.Y1 = parentPosition.Y;
line.X2 = childPosition.X;
line.Y2 = childPosition.Y;
line.Stroke = System.Windows.Media.Brushes.Black;
line.StrokeThickness = 2;
line.Tag = this.name;
lines.Add(line);
_surfaceWindow.RootGrid.Children.Add(line);
}
Upvotes: 4