3

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.

4

2 に答える 2

2

それがどのようにレンダリングされたかを見せてもらえますか? 新しいプロジェクトで xaml を試しましたが、何が問題なのかわかりません

于 2013-03-05T09:11:20.117 に答える
2

「InternalCanvas」要素がキャンバスのタイプであることは重要ですか? たとえば、代わりに Grid を使用すると、サンプルは正常に動作するためです。

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

編集:

この動作の理由は、 MSDNremarksのセクションで読むことができます。

キャンバスは、固有のレイアウト特性を持たない唯一のパネル要素です。Canvas が子要素のサイズを自動的に変更する要素の子でない限り、Canvas のデフォルトの Height プロパティと Width プロパティはゼロです。Canvas の子要素はサイズ変更されることはなく、指定された座標に配置されるだけです。これにより、固有のサイズ制約や位置合わせが不要または不要な状況に柔軟に対応できます。子コンテンツを自動的にサイズ変更して配置する場合は、通常、Grid 要素を使用するのが最適です。

于 2013-03-05T10:24:51.940 に答える