DermFrench
DermFrench

Reputation: 4057

Item height relative to actual row height with * WPF XAML

I have the following XAML

<Window x:Class="WpfApplication3.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:wpfApplication3="clr-namespace:WpfApplication3"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="60*"/>
            <RowDefinition Height="147*"/>
            <RowDefinition Height="112*"/>
        </Grid.RowDefinitions>
        <TabControl Grid.Row="1">
            <TabItem Header="Match Bets" >

                <StackPanel Margin="15" Orientation="Vertical" Height="Auto" Width="Auto" >
                    <Label Content="My Header"/>
                    <DataGrid 
                        Height="Auto" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Width="Auto">
                        <DataGrid.Columns>
                            <DataGridTextColumn  IsReadOnly="False" Binding="{Binding Subject}" Header="Original A"  Width="Auto" />
                            <DataGridTextColumn  IsReadOnly="False" Binding="{Binding PriceOriginalSelectionB}" Header="Original B"  Width="Auto" />
                            <DataGridTextColumn  IsReadOnly="False" Binding="{Binding PriceDerivedSelectionA}" Header="Derived A"  Width="Auto" />
                            <DataGridTextColumn  IsReadOnly="False" Binding="{Binding PriceDerivedSelectionB}" Header="Derived B"  Width="Auto" />
                        </DataGrid.Columns>

                        <wpfApplication3:Dummy Subject="Subject 1" Body="Body 1" />
                        <wpfApplication3:Dummy Subject="Subject 2" Body="Body 2" />
                        <wpfApplication3:Dummy Subject="Subject 2" Body="Body 2" />
                        <wpfApplication3:Dummy Subject="Subject 2" Body="Body 2" />
                        <wpfApplication3:Dummy Subject="Subject 2" Body="Body 2" />
                        <wpfApplication3:Dummy Subject="Subject 2" Body="Body 2" />

                    </DataGrid>

                </StackPanel>

            </TabItem>

        </TabControl>
        <DataGrid HorizontalAlignment="Stretch" Margin="5" Grid.Row="2" VerticalAlignment="Stretch" Height="Auto" Width="Auto" />

    </Grid>
</Window>

I want my datagrid (which is inside a stackpanel) only to grow to below the size of the row it is inside, but the stack panel seems to allow it to grow to any size it wants. Is there anyway without explicitly setting the height of the stack panel that I can do this. Or can I set the height of the stackpanel to be slightly smaller that the star * height of the row.

If I remove the stackpanel that the datagrid is inside this works fine, but I need the stackpanel to add other things above and below the datagrid.

Upvotes: 0

Views: 1685

Answers (2)

Use a grid. StackPanel behaves differently and Grid should allow "growing" as you want it.

Upvotes: 1

paul
paul

Reputation: 22001

Do not set the Height of the DataGrid, but instead set the VerticalAlign to Stretch, and use a 5px top/bottom margin.

Upvotes: 2

Related Questions