如何正确居中 WPF 画布?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18967827/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:42:49  来源:igfitidea点击:

how to properly center a WPF canvas?

c#wpf

提问by Frank

I have a full screen dialog and want to center a canvas. It seems to work well with a grid with Vertical/Horizontalalignment for a Label for example, but when I try this the top left corner gets centered instead of the middle of the canvas:

我有一个全屏对话框,想将画布居中。例如,它似乎适用于带有垂直/水平对齐标签的网格,但是当我尝试此操作时,左上角居中而不是画布中间:

<Window
    Title=""
    Topmost="True" WindowStyle="None" WindowState="Maximized"
    >
<Grid>
    <Grid VerticalAlignment="Center" HorizontalAlignment="Center">
        <Canvas>
            <Border Margin="20" 
                Background="White"
                BorderBrush="Black"
                BorderThickness="2"
                Padding="20" >
                <DockPanel Margin="10">
                    <StackPanel DockPanel.Dock="Top" Margin="0 0 0 50" 
                                Orientation="Vertical">
                       <Label FontSize="32" Content="Hello"></Label>
                    </StackPanel>
                    <StackPanel HorizontalAlignment="Right"
                                DockPanel.Dock="Bottom" 
                                Orientation="Horizontal">
                            <Label FontSize="32" Content="Hello"></Label>
                    </StackPanel>
                </DockPanel>
            </Border>
        </Canvas>
    </Grid>

回答by K Mehta

That's because you haven't set a Width and Height on your canvas, and in in the case of a canvas, the ActualWidth and ActualHeight properties (which are used for layout) default to 0.

那是因为您没有在画布上设置 Width 和 Height,对于画布,ActualWidth 和 ActualHeight 属性(用于布局)默认为 0。

You can test this by setting the Background on the Canvas - in your case, the background color won't be rendered because of the aforementioned reason.

您可以通过在画布上设置背景来测试这一点 - 在您的情况下,由于上述原因,背景颜色不会被渲染。

To work around this do one of the following:

要解决此问题,请执行以下操作之一:

  1. pick another container (Grid, for example) which adapts based on it's children's sizes
  2. set the Width and Height on the Canvas explicitly.
  1. 选择另一个容器(例如网格),它根据孩子的大小进行调整
  2. 明确设置画布上的宽度和高度。

回答by Tony

You can put the Border as child of Grid instead of Canvas. (The Grid can have more than one child) Try this:

您可以将 Border 作为 Grid 的子项而不是 Canvas。(网格可以有多个孩子)试试这个:

<Window             
    Title=""
    Topmost="True" WindowStyle="None" WindowState="Maximized"
    >
    <Grid>
        <Canvas HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
            <Rectangle Canvas.Left="40" Canvas.Top="31" Width="630" Height="41" Fill="Blue" />
            <Ellipse Canvas.Left="130" Canvas.Top="79" Width="580" Height="580" Fill="Blue" />
            <Path Canvas.Left="61" Canvas.Top="28" Width="133" Height="98" Fill="Blue" Stretch="Fill" Data="M61,325 L293,28" />
        </Canvas>
        <Border Margin="20" 
                HorizontalAlignment="Center"
                VerticalAlignment="Center"
                Background="WhiteSmoke"
                BorderBrush="Black"
                BorderThickness="2"
                Padding="20">
            <DockPanel Margin="10">
                <StackPanel DockPanel.Dock="Top" Margin="0 0 0 50" Orientation="Vertical">
                    <Label FontSize="32" Content="Hello" />
                </StackPanel>
                <StackPanel HorizontalAlignment="Right"
                                DockPanel.Dock="Bottom" 
                                Orientation="Horizontal">
                    <Label FontSize="32" Content="Hello" />
                </StackPanel>
            </DockPanel>
        </Border>

    </Grid>
</Window>