简体   繁体   中英

Set empty grid to fill space in a WPF grid

I've got a subclassed grid with a few extra bits and bobs and I've set it to show the gridlines. I made it so that the height and width of a child spans so many rows as well. I had it set to 16x16 which is set at the start and adds the rows and columns programmatically.

I've done this to set the height/width:

 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);
        }

I added children at the start which are 4x4 at one below each other, and that works fine, but it's only showing gridlines for the things that have stuff in it. When I add something new with a width of say 8, it shows gridlines for much smaller columns.

Mainly, I want the 16x16 grid to initially show an empty 16x16 grid on the screen with nothing on it except gridlines. Then when I add things at certain points, it'll add them at the right time and stuff. Is there something in particular that I'm doing?

EDIT: Added XAML for MainWindow.Xaml Cut out the irrelevant bits.

<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: Figured I should show the end result just in case people want a similar look.

I fear the issue is due to your layout: a StackPanel will give the Grid exactly the space it needs, no more no less.

So you should instead use a DockPanel (with the Grid as the last child) or even a Grid for the outer panel.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM