开发者

Resizable WPF Grid inserts blank lines between rows and columns depending on actual size

I have a Grid displaying rectangles. The Grid can be resized and is supposed to display adjacent rectangles without any gap.

However, depending on the actual size of the Grid, blank lines appear between the rows and columns of the Grid. I guess this is to make up for sizes where the width isn't a multiple of the number of columns, or the height isn't a multiple of the number of rows.

The following XAML code demonstrates this:

<Window x:Class="test.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition></ColumnDefinition>
        <ColumnDefinition></ColumnDefinition>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
     开发者_如何转开发   <RowDefinition></RowDefinition>
        <RowDefinition></RowDefinition>
    </Grid.RowDefinitions>
    <Rectangle Grid.Column="0" Grid.Row="0" Fill="DarkBlue"></Rectangle>
    <Rectangle Grid.Column="1" Grid.Row="0" Fill="DarkOrange"></Rectangle>
    <Rectangle Grid.Column="0" Grid.Row="1" Fill="DarkBlue"></Rectangle>
    <Rectangle Grid.Column="1" Grid.Row="1" Fill="DarkBlue"></Rectangle>
</Grid>

</Window>

How can I prevent those blank lines? If one or some of the columns/rows are slightly larger than others is not really a problem. I'd like to keep the markup simple.

I unsuccessfully tried Stretch, Fill, putting the Grid in a ViewBox, ShowGridLines=False, Width=Auto, etc.

An image of the issue is as follows:

Resizable WPF Grid inserts blank lines between rows and columns depending on actual size


Add SnapsToDevicePixels="True" to your Grid control

Pixel Snapping in WPF Applications

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜