How could I put a border on my grid control in WPF?
I think your problem is that the margin should be specified in the border tag and not in the grid.
If you just want an outer border, the easiest way is to put it in a Border control:
<Border BorderBrush="Black" BorderThickness="2">
<Grid>
<!-- Grid contents here -->
</Grid>
</Border>
The reason you're seeing the border completely fill your control is that, by default, it's HorizontalAlignment and VerticalAlignment are set to Stretch. Try the following:
<Grid>
<Border HorizontalAlignment="Left" VerticalAlignment="Top" BorderBrush="Black" BorderThickness="2">
<Grid Height="166" HorizontalAlignment="Left" Margin="12,12,0,0" Name="grid1" VerticalAlignment="Top" Width="479" Background="#FFF2F2F2" />
</Border>
</Grid>
This should get you what you're after (though you may want to put a margin on all 4 sides, not just 2...)
If nesting your grid in a border control
<Border>
<Grid>
</Grid>
</Border>
does not do what you want, then you are going to have to make your own control template for the grid (or border) that DOES do what you want.
This is a later answer that works for me, if it may be of use to anyone in the future. I wanted a simple border around all four sides of the grid and I achieved it like so...
<DataGrid x:Name="dgDisplay" Margin="5" BorderBrush="#1266a7" BorderThickness="1"...