tags:

views:

242

answers:

1

Is it possible to manipulate the controls tree of a WPF window programatically?

I have a string that contains an arbitrary WPF XAML code. I show the content in a content control. No Problem so far. Additionally I want to decorate every Grid control that's contained within the XAML snippet with let's say a special Border.

What are possible ways of doing this?

A: 

You can walk the control tree and, if the current control is of type Grid, add your border to it.

Here's some pseudocode that looks shockingly like C# and may actually compile and work:

private void AddGrid(Control c){
  foreach(var child in c.Children)
    AddGrid(child);
  if(this is Grid)
    this.Border = new Border(/* whatever */);
}

Alternatively, in the Resources of the control that contains your dynamic xaml, you can add a Style that alters the appearance of all Grids and adds the border you want around it. This is a good resource for learning how to do this. Just keep in mind that if you add the style to the window's resources or the application's resources it will affect all controls in your window or application, respectively. Adding it to the immediate parent of the dynamic xaml will (? never tried this before) affect only its child control's templates.

Will