I have made a simple UserControl called SmallCtrl. Inside another UserControl called LargeCtrl, I dynamically add SmallCtrl to the list of its children using this code behind (for testing):
public void LargeCtrl_Loaded(object sender, EventArgs args)
{
for (int i = 0; i < 10; i++)
_StackPanel.Children.Add(new SmallCtrl());
}
I use StackPanel with horizontal orientation and I need its child UserControls to have a right margin to create space between them, here is the style resource:
<Grid>
<StackPanel x:Name="_StackPanel" Orientation="Horizontal">
<StackPanel.Resources>
<Style TargetType="{x:Type customCtrls:SmallCtrl}">
<Setter Property="Margin" Value="0,0,10,0"/>
</Style>
</StackPanel.Resources>
</StackPanel>
</Grid>
However, when I load my Window with LargeCtrl the margin doesn't show. If I replace my SmallCtrl with a TextBox, everything works. What is my problem here?