tags:

views:

115

answers:

2

How do i put a border on my grid in C#/WPF?

This is what i would like it to be, but puts a border around the whole thing instead of the grid control i put in my application.

<Grid>
    <Border BorderBrush="Black" BorderThickness="2">
        <Grid Height="166" HorizontalAlignment="Left" Margin="12,12,0,0" Name="grid1" VerticalAlignment="Top" Width="479" Background="#FFF2F2F2" />
    </Border>
... and so on ...
+2  A: 

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>

Edit:

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...)

Reed Copsey
it does not work as i want... i've added some code to my question.
Jason94
@Jason94: I updated my answer to show you how to get what I think you're after...
Reed Copsey
A: 

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.

Muad'Dib
oh... okey :D thought there were a variable i overlooked or something fancy WPF (im new at it :D)
Jason94
You cannot create a Template for Grid and Border because they have no Template property as they are not derived from Control, but from Panel and Decorator. [Reed Copsey has the (pretty simple) solution](http://stackoverflow.com/questions/2769291/how-do-i-put-a-border-on-my-grid-in-wpf/2769312#2769312).
gehho