Martin Tausch
Martin Tausch

Reputation: 744

Canvas doesn't respect grid borders?

I have a simple grid.

<Grid>

    <Grid.RowDefinitions>
        <RowDefinition Height="35" />
        <RowDefinition Height="35" />
    </Grid.RowDefinitions>

    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="50" />
    </Grid.ColumnDefinitions>

    <Canvas Grid.Column="0" Grid.Row="0" Width="1600" Height="35" Background="Black" />
    <TextBlock Grid.Column="0" Grid.Row="1" Width="1600" FontSize="20"
               Text="this is a sample text which respects the borders of the grid"  />

</Grid>

which results in:

Canvas vs text

Now I really wonder why the Canvas is drawn over its column into the next column, while the text is not. I would expect this behaviour if I set its ColumnSpan-Property to at least 2. But this is not the case and the Canvas doesn't care about an explicit ColumnSpan either.

Is there any reason in this? Can I limit the Canvas to its column without cutting its width?

Upvotes: 0

Views: 275

Answers (2)

user1246682
user1246682

Reputation: 206

Workaround for this problem is to place canvas into other panel which respect parent width like StackPanel:

<StackPanel  Grid.Column="0"
                 Grid.Row="0"
                 Orientation="Horizontal">
        <Canvas Background="Black"
                Width="1600"
                Height="35" />
    </StackPanel>

or even when you place any item in first row second column, that item would be in foreground and canvas would be in background, hence not visible.

Upvotes: 1

Jawahar
Jawahar

Reputation: 4885

Your first ColumnDefinition Width is Star sized, which means it will take all the available width after allocating width to Auto sized columns. Sincerely your canvas does not drawn over the other column, instead its Width (1600px) is too high.

Upvotes: 0

Related Questions