I add some controls dynamically in a grid children. I will not know how many and I will not know the exact hierarchy. What I want to do is to access some controls that have a specific type (Button for example) and instantiate some properties. What is the best way to do this?
+1
A:
You can override button style at grid level. Here's an example:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="144*" />
<RowDefinition Height="171*" />
</Grid.RowDefinitions>
<Grid.Resources>
<!-- Override all Buttons style that are childs of the grid -->
<Style TargetType="Button">
<Setter Property="Background" Value="LightBlue"/>
<Setter Property="FontSize" Value="20" />
</Style>
</Grid.Resources>
<Button Grid.Row="0">Button1</Button>
<Button Grid.Row="1">Button2</Button>
</Grid>
in this example all buttons placed inside the grid have the background and font size set by the style defined in the grid resources.
Edit: this works for controls buttons added at runtime also, the style will be applied to those controls when added to the grid, and you can also define styles programaticaly if you want and add them to the grid resources and they will be applied automatically.
Pop Catalin
2009-04-08 10:31:44
A:
You'll have to walk your way through VisualTree using VisualTreeHelper. There's some good examples of how to get controls of a specific type
MrTelly
2009-04-08 10:38:14