以下方法在行中引发异常:
Point childPosition = vb.TransformToAncestor(surfaceWindow).Transform(new Point(0, 0));
但是如果你看一下代码,vb肯定是surfaceWindow的孩子。那么为什么这不起作用呢?
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);
}
编辑:我发现可能是我的问题的答案:
Error when using TransformToAncestor: "The specified Visual is not an ancestor of this Visual."
问题是,我不明白解决方案。谁能解释一下?编辑2:我试图实现这个调度程序。但仍然会抛出相同的异常。任何提示都会非常棒!
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);
}
答案 0 :(得分:4)
我终于能够解决问题了。似乎在调用
之后ScatterViewItem尚未添加到VisualTree中surfaceWindow.ClassScatter.Item.Add(vb);
所以我在ScatterViewItem中添加了一个SizeChangedEventHandler和一个ScatterManipulationDeltaEventHandler,并在那里添加了这些行。
感谢Bart,他帮我解决了这个问题
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);
}
和
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);
}
答案 1 :(得分:0)
我在将项目列表分配给 DataGrid 时遇到了同样的错误。原来如果列表为空,即使列表不为空。
要解决这个问题,添加验证就足够了:
if (listOfObjectsToShowInTheGrid?.Any() ?? false)
{
this.DataGrid1.ItemsSource = listOfObjectsToShowInTheGrid;
this.DataGrid1.UpdateLayout();
}
else
{
// Do somethig if the list is empty
}