用户控件的最后一个组件未填充托管网格

时间:2015-08-12 15:55:49

标签: c# wpf xaml

我正在开发一个C#wpf项目,我遇到了问题。我有一个主窗口,主机网格。此网格承载用户控件,用户控件以3列布局托管3个DockPanel。

3列可通过网格分割器调整,但是右侧列不填充主窗口网格的可用空间。

以下是托管用户控件的主窗口的XAML:

<Window x:Class="Boardies_Email_Client.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Boardies_Email_Client"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" WindowStartupLocation="CenterScreen" ResizeMode="CanResizeWithGrip" WindowState="Maximized">
    <Grid>
        <Grid x:Name="GridHost" Margin="0" Background="#FFF3FF00"/>
        <StatusBar Height="24" VerticalAlignment="Bottom"/>
    </Grid>
</Window>

以下是用户控件的XAML

<UserControl x:Class="Boardies_Email_Client.EmailClient"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:Boardies_Email_Client"
             mc:Ignorable="d" 
             d:DesignHeight="505">
    <Grid HorizontalAlignment="Left" Width="1120">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="92*"/>
            <ColumnDefinition Width="121*"/>
            <ColumnDefinition Width="87*"/>
            <ColumnDefinition Width="225*"/>
            <ColumnDefinition Width="134*"/>
            <ColumnDefinition Width="116*"/>
            <ColumnDefinition Width="345*"/>
        </Grid.ColumnDefinitions>
        <DockPanel Background="#FFEC1717" Grid.ColumnSpan="2">
            <Label x:Name="label" Content="My Label" Margin="0,0,0,460" Width="175"/>
        </DockPanel>
        <GridSplitter x:Name="gridSplitter" HorizontalAlignment="Left" Width="5" Grid.Column="2"/>
        <DockPanel LastChildFill="False" Margin="5,0,0,0" Background="#FF5DFF00" Grid.Column="2" Grid.ColumnSpan="3"/>
        <GridSplitter x:Name="gridSplitter1" HorizontalAlignment="Left" Width="5" Grid.Column="5"/>
        <DockPanel Grid.Column="5" Margin="5,0,0,0" Background="#FF0A00E2" Grid.ColumnSpan="2"/>
    </Grid>
</UserControl>

以下是显示问题的屏幕截图

Screenshot showing problem

红色,绿色和蓝色是由网格分割器分隔的停靠面板。黄色是托管用户控件的主窗口网格的背景颜色,蓝色面板应该填满黄色的可用空间,但我看不出它为什么不是。

我没有硬设置任何我能找到的宽度。

1 个答案:

答案 0 :(得分:0)

@Boardy

我运行了你的代码。 分割UserControl和Grid的组织都很好。

但是您将宽度设置为固定大小:

<UserControl ...>
    <Grid HorizontalAlignment="Left" Width="1120">

如果你删除固定大小,一切都很好,控件采用窗口大小,右边没有看到大于1120的其他背景(黄色或右边):

<UserControl ...>
    <Grid >

此致