设置空网格以填充WPF网格中的空间

时间:2014-08-12 15:09:23

标签: c# wpf layout grid space

我有一个带有一些额外位和bobs的子网格,我已将其设置为显示网格线。我做到了这样,孩子的身高和宽度也跨越了很多行。我把它设置为16x16,它在开始时设置并以编程方式添加行和列。

我这样做是为了设置高度/宽度:

 for (int i = 0; i < width; i++)
        {
            ColumnDefinition col = new ColumnDefinition();
            col.Width = new GridLength(1, GridUnitType.Star);
            ColumnDefinitions.Add(col);
        }
        for (int i = 0; i < height; i++)
        {
            RowDefinition row = new RowDefinition();
            row.Height = new GridLength(1, GridUnitType.Star);
            RowDefinitions.Add(row);
        }

我在开始时添加了4x4的孩子,一个在彼此之下,这样可以正常工作,但它只显示有内容的东西的网格线。当我添加宽度为8的新内容时,它会显示更小列的网格线。

主要是,我希望16x16网格最初在屏幕上显示一个空的16x16网格,除了网格线外没有任何内容。然后,当我在某些点添加东西时,它会在正确的时间添加它们。我正在做什么特别的事情?

编辑:MainWindow.Xaml添加了XAML,删除了无关的位。

<Window x:Class="OHS.MainWindow"
    Height="440" Width="867" SizeToContent="Manual" DataContext="{Binding}" ResizeMode="NoResize" WindowStyle="SingleBorderWindow">

<StackPanel Height="Auto" Name="stackPanel1" Width="Auto" Orientation="Horizontal" HorizontalAlignment="Stretch">
        <local:DControl ShowGridLines="True" x:Name="control" Background="#FFDEDEDE" height="16" width="16" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
    <StackPanel Height="Auto"  Name="optionPanel" Width="Auto" HorizontalAlignment="Right">
        <GroupBox Height="Auto" Name="groupBox1" Width="Auto">
            <Canvas Height="Auto" Name="canvas1" Width="Auto" HorizontalAlignment="Right">
                <Label Canvas.Left="6" Canvas.Top="33" Content="Type of Chart: " Height="23" Name="label1" Width="119" FontFamily="Calibri" />
                <ComboBox ItemsSource="{Binding Source={en:Enumeration {x:Type en:ChartType}}}" DisplayMemberPath="Description" SelectedValue="{Binding currentChartType}" SelectedValuePath="Value" Canvas.Left="144" Canvas.Top="33" Height="23" Name="chartCombo" Width="123" DataContext="{Binding}" />
                <Button Content="Add" Height="23" Name="addButton" Width="104" Canvas.Left="163" Canvas.Top="190" Click="button1_Click" />
                <Label Content="Stored Procedure: " Height="23" Name="label2" Canvas.Left="6" Canvas.Top="65" />
                <ComboBox Canvas.Left="144" Canvas.Top="65" Height="23" Name="storedProcCombo" Width="123" SelectionChanged="storedProcCombo_SelectionChanged" />
                <Label Canvas.Left="6" Canvas.Top="6" Content="Title:" FontFamily="Calibri" Height="23" Name="label3" Width="119" />
                <Label Canvas.Left="6" Canvas.Top="94" Content="Update Frequency (sec): " Height="25" Name="label4" />
                <TextBox Height="23" Name="titleTextBox" Width="123" Canvas.Left="144" Canvas.Top="6" />
                <TextBox Canvas.Left="144" Canvas.Top="96" Height="23" Name="freqTextBox" Width="123" PreviewTextInput="freqTextBox_PreviewTextInput" />
                <Label Canvas.Left="6" Canvas.Top="128" Content="Position" Height="25" Name="label5" />
                <TextBox Canvas.Left="160" Canvas.Top="130" Height="23" Name="xTextBox" Width="34" PreviewTextInput="xTextBox_PreviewTextInput" />
                <Label Canvas.Left="144" Canvas.Top="128" Content="X: " Height="25" Name="label7" Width="23" />
                <Label Canvas.Left="213" Canvas.Top="128" Content="Y:" Height="25" Name="label8" />
                <TextBox Canvas.Left="233" Canvas.Top="131" Height="23" Name="yTextBox" Width="34" PreviewTextInput="yTextBox_PreviewTextInput" />
                <Label Canvas.Left="6" Canvas.Top="159" Content="Height: " Height="25" Name="label6" />
                <TextBox Canvas.Left="63" Canvas.Top="161" Height="23" Name="heightTextBox" Width="62" PreviewTextInput="heightTextBox_PreviewTextInput" />
                <Label Canvas.Left="144" Canvas.Top="159" Content="Width: " Height="25" Name="label9" />
                <TextBox Canvas.Left="205" Canvas.Top="161" Height="23" Name="widthTextBox" Width="62" PreviewTextInput="widthTextBox_PreviewTextInput" />
            </Canvas>
        </GroupBox>

    </StackPanel>
</StackPanel>

EDIT2:想想我应该展示最终结果,以防人们想要一个类似的外观。

1 个答案:

答案 0 :(得分:2)

我担心这个问题是由于你的布局造成的:StackPanel会给Grid提供所需的空间,而不是更少。

因此,您应该使用DockPanelGrid作为最后一个孩子)或外部小组Grid