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

wpf grid cell border
wpf grid border programmatically
wpf grid lines
wpf border around grid
xaml grid> </border
wpf window border
wpf border fill color
wpf border style

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

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

The Border control, We investigate the Border control, which you may use to draw a border, a background, Width="200"> <Grid Margin="10"> <Border Background="​GhostWhite"  How can I achieve a dashed or dotted border in WPF? Ask Question Asked 8 years, 10 months ago. Active 10 months ago. How do i put a border on my grid in WPF? 7.

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.

Border around Grid Panel - MSDN, First, create a WPF application. In the Toolbox, select Border and drag it to your window in the designer. Now, change the Border attributes within the XAML file. BorderBrush: We can set the BorderBrush to a named color. Border. A program interface needs visual clues. The Border control in WPF provides a way to organize an interface into parts. And each part can have a different purpose or use. We use Border to draw colored, visual borders within a Window.

I think your problem is that the margin should be specified in the border tag and not in the grid.

WPF Border Example, To place a border around an element, WPF provides the Border element. Similar to other WPF elements, the Border has Width, Height,  Borders in WPF works little differently. Border in XAML is its own control that can be applied to other controls or XAML elememts. To place a border around an element, WPF provides the Border element. Similar to other WPF elements, the Border has Width, Height, Background, and HorizontalAlignment and VerticalAlignment properties.

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

WPF Border Tutorial, This enum is used to set the visibility of grid lines which can be either Brush called GridLineBrush which sets the color of GridLines Border. A Grid is a very powerful and useful Layout in WPF. It enables you to arrange children elements in cells defined by rows and columns. In fact, when we add a new XAML document or create a new WPF Project in Visual Studio, Visual Studio automatically adds a Grid as the first container inside the window element.

<Grid x:Name="outerGrid">
    <Grid x:Name="innerGrid">
        <Border BorderBrush="#FF179AC8" BorderThickness="2" />
        <other stuff></other stuff>
        <other stuff></other stuff>
    </Grid>
</Grid>

This code Wrap a border inside the "innerGrid"

WPF Grid Control with Solid GridLines, properties on Button, Grid and StackPanel to set the border directly. harinikmsft added this to the XAML Standard 1.0 milestone on May 9,  Some developers who make the transition from Windows Forms to WPF often try to do the same in a WPF application. What they don’t know (yet) is that this approach is generally the wrong way to take.

Add Border* properties on Button, Grid, StackPanel · Issue #13 , 1 >> Use the LoadedCellPresenter event to set Border and/or BackGround of any As Object, e As C1.WPF.DataGrid.DataGridCellEventArgs) Handles <Grid x:​Name="Root" Margin="{TemplateBinding CellsAreaMargin}"> Any control in WPF can be placed within a grid by using its Grid.Row and Grid.Column properties that represents what column and what row a control will be placed in. The values of rows and columns start with 0. That means, if there are three columns in a grid, the first column will be represented by the number 0.

border or background for current cell or row, The code examples in this article, you will learn how to use a WPF Grid Panel Set only the bottom border style of each row in a gridview to 20 pixels w How to  Is it possible to add a border to a textblock. I need it to be added in the setter property below code: Use a TextBox instead. – Jim Balter Apr 15 '16 at 3:24. No, you need to wrap your TextBlock in a Border. Of course, you can set these properties ( BorderThickness, BorderBrush) through styles as well: A TextBlock does not actually inherit

Grid line style wpf, The undefined behaviors of WPF Grid (the so-called bugs) Then what's the visible width of the 2nd Border and the 3rd Border ? Press. F5. in. Visual So now, we remove the Canvas and use HorizontalAlignment="Right" . Hello guys, I want set border on each row so how it is possible in grid, if you have any idea about, then share please, i have tried lots of code but did not find any solution. This site uses cookies for analytics, personalized content and ads.

Comments
  • What do you mean by "whole thing"? I suspect you have something else inside your grid - you should perhaps post what that is. My suspicion is that you have some kind of datagrid control as well as your grid container (which is what you've put in your sample and would presumably contain the "whole thing") and that's where the confusion arises.
  • it does not work as i want... i've added some code to my question.
  • @Jason94: I updated my answer to show you how to get what I think you're after...
  • @ReedCopsey Reed, I know your solution works, what I don't understand is why the Border element is inside the Grid element - isn't this counter intuitive? Thank you.
  • @Sabuncu You'll notice that my original didn't have that - I was making the OP's edit "work"
  • This seems to place a boarder around the inside of my main window and not around the outside of the grid that is encapsulated within it. Any ideas why?
  • oh... okey :D thought there were a variable i overlooked or something fancy WPF (im new at it :D)
  • 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.
  • The real answer.
  • Hi @ElvinMammadov. Can you describe more. Are you getting any errors? This code works fine to me.
  • Need to add Grid.ColumnSpan="the columns you want to cover" Grid.RowSpan= "The rows you want to cover" in Border tag. Otherwise it only covers the first cell.