Stacklurker
Stacklurker

Reputation: 55

wpf canvas won't resize

I am trying to learn WPF, but I have reached an impasse. The problem is that I have a control derived from UIControl, relevant parts:

    <Grid>
    <Border x:Name="OuterBorder" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" BorderThickness="2" BorderBrush="Black">
        <Canvas x:Name ="InternalCanvas" Width="Auto" Height="Auto" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="Ivory">
        </Canvas>
    </Border>
</Grid>

This gives me a little black dot, when I render the control; I assume this is the border.

I then add code to the constructor after InitizializeComponents:

        Label l = new Label(); 
        l.Content = "HELLO"; 
        l.BorderThickness = new Thickness(2);
        l.BorderBrush = Brushes.Bisque;
        if (this.GetType() == typeof(SO.SOGraphNode)) 
            this.InternalCanvas.Children.Add(l);

The intent of the code is that if the class is not inherited, it should show some kind of placeholder. Which it does.

The problem is that InternalCanvas and it's ecplipsing OuterBorder won't resize around the newly created nice label.

The border remains a dot-border and InternalCanvas' Ivory background is not seen behind the label.

I have googled this alot, beliving that I needed to refresh or update the canvas/control elements, but I'm not so sure anymore. Mainly through watching a lot of Dispatcher.Invoke variations, which I have applied very liberally through out the code at different points, every time with no change in behavior.

WPF is a bit oblique to me still, if anyone knows how I could resolve this problem I do indeed have a shiny "Correct Answer" to dole out. :)

Edit 1 - Screenshot of the results:

No resize for this Canvas!

Notice the general lack of Ivory background and OuterBorder still remains a small artifact in the topright corner of the control while the childelement is obviously much larger.

Upvotes: 3

Views: 1489

Answers (2)

rhe1980
rhe1980

Reputation: 1577

Is it important for you that the "InternalCanvas" element is type of canvas? Because if you use for example a Grid instead, the sample works fine:

<Border x:Name="OuterBorder" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" BorderThickness="2" BorderBrush="Black">
        <Grid x:Name ="InternalGrid" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="Ivory">
        </Grid>
    </Border>

edit:

The reason for this behaviour you can read in the remarks section in MSDN:

Canvas is the only panel element that has no inherent layout characteristics. A Canvas has default Height and Width properties of zero, unless it is the child of an element that automatically sizes its child elements. Child elements of a Canvas are never resized, they are just positioned at their designated coordinates. This provides flexibility for situations in which inherent sizing constraints or alignment are not needed or wanted. For cases in which you want child content to be automatically resized and aligned, it is usually best to use a Grid element.

Upvotes: 2

Daniel
Daniel

Reputation: 9521

Can you show us how it rendered? I tried your xaml in a new project and I can't figure out what the problem is

Upvotes: 2

Related Questions