Dynamically generated Silverlight controls are not displayed

I am dynamically adding custom controls in Silverlight 2, but they are not visible. However, when I view the visual tree in Silverlight Spy, I note that their visibility property is visible and toggles it to "Hard" and back to "Visible" so that the controls become visible.

What could I be doing wrong? Any ideas on what I should be looking for to solve this problem?

+1


a source to share


4 answers


I was able to solve the problem that happened when I was dynamically adding time periods to the timeline.

The controls that I dynamically add to the canvas have the following structure.

<Path x:Name="ribbonItem" Fill="Green">
  <Path.Data>
    <GeometryGroup>
      <RectangleGeometry x:Name="ribbonItemBackground" />
    </GeometryGroup>
  </Path.Data>
</Path>

      

The dimensions of the RectangleGeometry depend on the three inputs, the dates and time ranges of the timeline bar and timeline element (which I add) and the dimensions of the panel (canvas).

The following method is called when any properties of the BarRange, ItemRange, or BarSize object have been set.



private void Resize()
{
if (_itemRange != null && _barRange != null && _barSize != Size.Empty)
  {
    ribbonItemBackground.Rect = ItemRectangle();
  }
}

      

This caused the drawing problem mentioned in my question and the solution was to add

ribbonItem.InvalidateMeasure();

      

immediately after installing ribbonItemBackground.Rect.

+1


a source


UpdateLayout () in ParentControl is not required.

But don't forget my_parent_control.Add (UIElement mynewcontrol);



i.e. my_parent_control can be a stack panel.

+2


a source


The first thing that comes to mind is what container object are you using to create dynamic controls, if any?

I could see how perhaps adding controls without using a container could cause some problems. I can't say that I created a lot of control objects dynamically, but I think adding your items to the stack would be the simplest implementation.

A good test case would be creating a grid and assigning grid locations to your controls when you create them.

Here's a link on how to do it.

+1


a source


Have you tried calling UpdateLayout () on the control you are adding to?

+1


a source







All Articles