日期:2014-05-18  浏览次数:21134 次

C# .Net为什么把Shape加到VisualCollection不显示
各位仁兄,我现在要把一个Shape rect元素加入到一个从Adorner继承过来的类BaseGraphTool中,BaseGraphTool中用一个VisualCollection对象存储这个rect,然后我又要把A加入到另一个从Adorner继承过来的类BaseManipulator中,但是现在的问题是类BaseManipulator显示了,但加到类BaseManipulator中的BaseGraphTool却没有显示,希望各位仁兄给出高见,在下感激涕零。以下是具体代码:
  public class BaseGraphTool:Adorner
  {
  protected VisualCollection visualChildren;

  public BaseGraphTool():base(new UIElement())
  {
  Initialize();
  }
  public BaseGraphTool(UIElement e):base(e)
  {
  Initialize();
  }
  void Initialize()
  {
  visualChildren=new VisualCollection(this);
  visualChildren.Clear();
  Rectangle rect = new Rectangle();
  Canvas.SetLeft(rect, 0);
  Canvas.SetTop(rect, 0);
  rect.Width = 50;
  rect.Height = 20;
  rect.Fill = Brushes.Red;
  rect.Stroke = Brushes.Black;
  rect.Stretch = Stretch.Fill;
  visualChildren.Add(rect);
  }

  protected override int VisualChildrenCount
  {
get 

return visualChildren.Count;
}
  }
  protected override Visual GetVisualChild(int index)
  {
  return visualChildren[index];
  }
  }
/////////////////////////////////////////////////////////////////////////////////////////

  public class BaseManipulator :Adorner
  {
   
  /// <summary>
  /// Container for manipulator controlls
  /// </summary>
  protected VisualCollection visualChildren;
  /// <summary>
  /// 
  /// </summary>
  protected System.Windows.Controls.Canvas mainCanvas;
  /// <summary>
  /// 
  /// </summary>
  /// <param name="element"></param>
  public BaseManipulator(UIElement element)
  : base(element)
  {
  if (!(AdornedElement.RenderTransform is TransformGroup))
  {
  TransformGroup t = new TransformGroup();
  t.Children.Add(new MatrixTransform());
  t.Children.Add(new RotateTransform());
  AdornedElement.RenderTransform = t;
  AdornedElement.RenderTransformOrigin = new Point(0.5, 0.5);
   
  }
  ThumbsResources tr = new ThumbsResources();
  tr.InitializeComponent();
  Resources = tr;
  this.Visibility = Visibility.Collapsed;  
  visualChildren = new VisualCollection(this);
  BaseGraphTool b=new BaseGraphTool();
visualChildren.Add(b);
   
  }
  /// <summary>
  /// 
  /// </summary>
  protected override int VisualChildrenCount { get { return visualChildren.Count; } }
  /// <summary>
  /// 
  /// </summary>
  /// <param name="index"></param>