Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 帆布不';不尊重网格边界?_C#_Wpf_Grid_Wpf Controls - Fatal编程技术网

C# 帆布不';不尊重网格边界?

C# 帆布不';不尊重网格边界?,c#,wpf,grid,wpf-controls,C#,Wpf,Grid,Wpf Controls,我有一个简单的网格 <Grid> <Grid.RowDefinitions> <RowDefinition Height="35" /> <RowDefinition Height="35" /> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="*

我有一个简单的网格

<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>

其结果是:

现在我真的想知道为什么画布会在它的列上画到下一列,而文本不是。如果我将其ColumnSpan属性设置为至少2,则会出现这种行为。但事实并非如此,画布也不关心显式的列跨度


这有什么原因吗?我可以将画布限制为其列而不剪切其宽度吗?

您的第一个列定义宽度为星形,这意味着在为自动调整大小的列分配宽度后,它将采用所有可用的宽度。诚恳地说,您的画布没有在另一列上绘制,而是其宽度(1600px)太高。

解决此问题的方法是将画布放置到其他面板中,该面板应考虑父级宽度,如StackPanel:

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


或者,即使在第一行第二列中放置任何项目,该项目也将位于前景,画布将位于背景中,因此不可见。

我可以将textelement的宽度设置为“1600”,右侧的间隙(第二列的宽度为“50”)仍然存在……将宽度设置为TextBlock根本无法执行任何操作,如果文本长度较小。正如您在代码中看到的,文本在右侧被截断,而画布则没有。是的,可以工作。但我真的很想知道它为什么会这样^^