Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grid Border / Gap between cells

Tags:

c#

wpf

xaml

grid

cells

I've created a ControlTemplate that contains a Grid with two rows.

Sadly, there appears to be a single pixel gap between the cells.

How do I remove the gap?

Here's a screenshot showing the gap:

Gap between cells

...and here's the XAML:

<Window x:Class="MAQButtonTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="695" Width="996">        
    <Window.Resources>
        <Style TargetType="TextBlock">
            <Setter Property="OverridesDefaultStyle" Value="True"/>
        </Style>
        <ControlTemplate TargetType="Button" x:Key="ButtonTemplate">
            <Grid Width="444" Margin="0" ShowGridLines="False">
                <Grid.RowDefinitions>
                    <RowDefinition Height="51" />
                    <RowDefinition Height="36" />
                </Grid.RowDefinitions>
                <Grid Grid.Row="0" Background="#286c97">
                    <TextBlock>This is the first piece of text</TextBlock>
                </Grid>
                <Grid Grid.Row="1" Background="#5898c0">
                    <ContentPresenter Grid.Row="0" />
                </Grid>
            </Grid>
        </ControlTemplate>
    </Window.Resources>
    <Grid>

        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="300" />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <Grid Grid.Column="0" Background="#e9f1f6"></Grid>
        <Grid Grid.Column="1" Background="#d2e3ed">
            <StackPanel>
                <TextBlock FontFamily="Segoe UI" FontSize="22" FontWeight="Medium" Margin="52,58,0,0" Foreground="#0c3d5d">Your Quizzes <TextBlock FontFamily="Segoe UI" FontSize="18" FontWeight="Medium" Foreground="#0c3d5d">(7)</TextBlock></TextBlock>
                <Grid Margin="0,20,0,0">
                    <Button Width="444" Background="{x:Null}" BorderThickness="0" Template="{StaticResource ButtonTemplate}" Click="DoSomething" BorderBrush="#032135">
                        <TextBlock Margin="6,2.8,0,0" FontFamily="Segoe UI" FontSize="19" Foreground="#032135" FontWeight="Medium">This is a second piece of text</TextBlock>
                    </Button>
                </Grid>
            </StackPanel>
        </Grid>

    </Grid>
</Window>
like image 375
Luke Avatar asked May 23 '12 11:05

Luke


People also ask

Can you style grid-gap?

grid-gap is the idiomatic approach for spacing grid items, but it's not ideal since grid gaps are just that: empty space, not physical boxes. To that end, the only way to color these gaps is to apply a background color to the grid container.

How do you make a row-gap in grid?

The grid-row-gap property in CSS is used to define the size of the gap between the grid elements. The user can specify the width of the gap separating the rows by providing value to the grid-row-gap.

Why are there gaps in my grid CSS?

The vertical gaps are caused by the images not filling the vertical space in the grid items. The problem is made worse with align-items: center on the container, which removes the align-items: stretch default. Essentially, there are no gaps between grid items.

How do you put space between grid items?

grid-column-gap or grid-gap CSS Grid provides the grid-column-gap and grid-row-gap properties. The shorthand for both is grid-gap . These properties allow you to create space between grid items.


1 Answers

Just add SnapsToDevicePixels="True" in your template grid

<ControlTemplate TargetType="Button" x:Key="ButtonTemplate">
                <Grid Width="444" Margin="0" ShowGridLines="False" SnapsToDevicePixels="True">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="51" />
                        <RowDefinition Height="36" />
                    </Grid.RowDefinitions>
                    <Grid Grid.Row="0" Background="#286c97">
                        <TextBlock>This is the first piece of text</TextBlock>
                    </Grid>
                    <Grid Grid.Row="1" Background="#5898c0">
                        <ContentPresenter Grid.Row="0" />
                    </Grid>
                </Grid>
            </ControlTemplate>
like image 64
Bilal Hashmi Avatar answered Oct 02 '22 22:10

Bilal Hashmi