开发者

how to get the height of multiline textbox increased automatically as form get stretched vertically in WPF

开发者 https://www.devze.com 2023-04-12 11:57 出处:网络
I m using following hierarchy WPF form - Grid - GroupBox - Grid -multiline textbox can you tell me howto get the height of multiline textbox increased automatically as the form gets stretched ve

I m using following hierarchy

 WPF form 
   - Grid
     - GroupBox
        - Grid 
          -  multiline textbox

can you tell me how to get the height of multiline textbox increased automatically as the form gets stretched vertically in WPF. Form is resizable by the user.

   <Window x:Class="WPFApp.UserFunctions"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        xmlns:rp="clr-namespace:WPFApp" 
        WindowStartupLocation="CenterScreen"
        ResizeMode="CanResize"
        Title="User Functions" ShowInTaskbar="False" 
        MinWidth="770" MinHeight="570" Width="770" Height="550"
        Icon="/WPFApp;component/images/Globe.png" 
        Name="frmUserFunctions" Closing="OnWindowClosing">
        <Grid Margin="2" VerticalAlignment="Stretch" Height="auto">
            <Grid.Resources>
                <ResourceDictionary>
                    <ResourceDictionary.MergedDictionaries>
                        <ResourceDictionary Source="../Style/StyleDictionary.xaml"/>
                    </ResourceDictionary.MergedDictionaries>
                </ResourceDictionary>
            </Grid.Resources>
            <Grid.RowDefinitions>
                <RowDefinition Height="auto" />
                <RowDefinition Height="auto" MinHeight="40" />
                <RowDefinition Height="auto" MinHeight="40" />
                <RowDefinition Height="auto" MinHeight="40" />
                <RowDefinition Height="auto" MinHeight="40" />
                <RowDefinition Height="auto" MinHeight="40" />
                <RowDefinition Height="auto" MinHeight="40" />
                <RowDefinition Height="auto" MinHeight="40" />
            </Grid.RowDefinitions>
            <Label Name="lblErrMsgDesp" Foreground="Red" FontWeight="Bold" Visibility="Collapsed" 
                    Content="Error"   Margin="5,5,5,5" Grid.Row="0" ></Label>

            <GroupBox Name="grpCFL" Margin="2" Width="auto" VerticalAlignment="Top" Grid.Row="1" Grid.RowSpan="2">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition/>
                        <RowDefinition/>
                    </Grid.RowDefinitions>
                    <StackPanel Orientation="Vertical" Margin="2" Width="auto" Grid.Row="0" VerticalAlignment="Top">
                        <StackPanel Margin="2" Width="auto">
                            <TextBlock Name="tbCFLStaticMessage" Text=""
                                       Foreground="Black"  Margin="2" ></TextBlock>
                        </StackPanel>
                        <StackPanel Orientation="Horizontal" Margin="2" Width="auto">
                            <Button  Name="btnOpenCFL" Style="{DynamicResource btnMainNew}" 
                                                 Click="OnClickCFLBrowse" Margin="5" />

                            <Button  Name="btnNewCFL" Style="{DynamicResource btnMainNew}" 
                                                 Click="OnClickNewCFL" Margin="5" />

                            <Button  Name="btnSaveCFL" Style="{DynamicResource btnMainNew}"
                                                 Click="OnClickSaveCFL" Margin="5" />
                        </StackPanel>
                    </StackPanel>

                    <StackPanel Orientation="Vertical" Margin="2" Width="auto" Grid.Row="1" 
                                   VerticalAlignment="Top">
                        <TextBlock Name="tbCFLFilePath" Foreground="Black"  Margin="2" ></TextBlock>
                        <Border BorderThickness="1" BorderBrush="LightGray" Margin="2">
                            <TextBlock MinWidth="660"  IsEnabled="False" Margin="1" Name="tbCFLFileName" 
                                       FontSize="13" Foreground="Black" MinHeight="20"/>
                        </Border>
                    </StackPanel>

                </Grid>
            </GroupBox>
            <GroupBox Name="grpFunctions"  Margin="2" VerticalAlignment="Stretch" Height="auto"
                      HorizontalAlignment="Stretch" Grid.Row="3" Grid.RowSpan="5">
                <Grid  VerticalAlignment="Stretch" Height="auto">
                    <Grid.RowDefinitions>
                        <RowDefinition/>
                        <RowDefinition/>
                        <RowDefinition/>
                        <RowDefinition/>
                        <RowDefinition/>
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>

                    <Border BorderThickness="0" BorderBrush="LightGray" Margin="2" Grid.Row="0">
                        <Grid Margin="2" >
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="auto"/>
                                <ColumnDefinition Width="auto"/>
               开发者_JS百科             </Grid.ColumnDefinitions>

                            <StackPanel Orientation="Horizontal" >
                                <Button Style="{DynamicResource btnMainNew}"  
                                HorizontalAlignment="Left" Name="btnNew" Margin="2,5,5,5" IsEnabled="False"
                                Click="OnClickNew"></Button>
                                <Button Style="{DynamicResource btnMainNew}" 
                                HorizontalAlignment="Right" Name="btnEdit" Margin="5,5,5,5"  IsEnabled="False"
                                Click="OnClickEdit"></Button>
                                <Button Style="{DynamicResource btnMainNew}" 
                                HorizontalAlignment="Right" Name="btnDelete" Margin="5,5,5,5"  IsEnabled="False"
                                Click="OnClickDelete"></Button>
                            </StackPanel>
                            <StackPanel Grid.Column="1" Orientation="Horizontal" 
                                HorizontalAlignment="Right" Margin="2">

                                <Button Style="{DynamicResource btnMainNew}" Content="Clear"
                                HorizontalAlignment="Right" Name="btnReset" Margin="5,5,5,5" 
                                Click="OnClickReset"/>

                                <Button Style="{DynamicResource btnMainNew}" Content="Close" Click="OnClickClose"
                                HorizontalAlignment="Right" Name="btnCancel" Margin="5,5,5,5" />
                            </StackPanel>
                        </Grid>
                    </Border>

                    <StackPanel Grid.Row="1" Margin="2" Orientation="Vertical" Name="spSelectFunction"
                      Width="auto"  VerticalAlignment="Top">
                        <TextBlock Name="tbSelectFunction" Foreground="Black" Margin="5,2,2,2" Width="auto"/>
                        <ComboBox Style="{DynamicResource cboBox}" IsEnabled="False" Margin="5,2,2,2" MaxWidth="1650"
                          Name="cmbFunctionsList" SelectionChanged="OnFunctionsListSelectionChanged"
                      VerticalAlignment="Center"  MinHeight="25"  Width="auto"/>
                    </StackPanel>

                    <StackPanel Orientation="Vertical" Grid.Row="1" Name="spFunctionName"  Margin="2" 
                        VerticalAlignment="Top" Visibility="Collapsed">
                        <TextBlock Name="tbFunctionName" Foreground="Black"  Width="auto"  Margin="5,2,2,2"></TextBlock>
                        <TextBox  Style="{DynamicResource txtBox}"  Width="auto"
                                 AcceptsReturn="False" Name="txtFunctionName" VerticalAlignment="Center" 
                                 TextChanged="OnFunctionNameTextChanged" Margin="5,2,2,2" MaxWidth="1650"
                                 VerticalScrollBarVisibility="Auto" TextWrapping="Wrap"  />
                    </StackPanel>

                    <StackPanel Grid.Row="2"  Margin="2" Orientation="Vertical" >
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition  Width="115"/>
                                <ColumnDefinition  Width="115"/>
                                <ColumnDefinition  Width="115"/>
                                <ColumnDefinition  Width="30"/>
                                <ColumnDefinition  />
                            </Grid.ColumnDefinitions>
                            <Grid.RowDefinitions>
                                <RowDefinition/>
                                <RowDefinition/>
                                <RowDefinition/>
                            </Grid.RowDefinitions>

                            <TextBlock Name="tbDataTypes" Foreground="Black"  Grid.Column="0" Grid.Row="0" Margin="5,2,2,2" >
                            </TextBlock>
                            <TextBlock Name="tbOccurences" Foreground="Black"  Grid.Column="1" Grid.Row="0" Margin="5,2,2,2">
                            </TextBlock>
                            <TextBlock Name="tbParametersSize" Foreground="Black"  Grid.Column="2" Grid.Row="0" Margin="5,2,2,2">
                            </TextBlock>
                            <TextBlock Name="tbGeneratedSignatureLabel" Foreground="Black"  Grid.Row="0" Grid.Column="3" Grid.ColumnSpan="2" Margin="5,2,2,2" />
                            <ComboBox Name="cmbDataTypes" Grid.Row="1" Grid.Column="0" Margin="5" 
                              Width="105" Style="{DynamicResource cboBox}" ItemsSource="{Binding}"
                              SelectionChanged="OncmbDataTypesSelectionChanged" MinHeight="25" 
                              MaxWidth="105" MinWidth="105">
                            </ComboBox>

                            <ComboBox Name="cmbOccurences" Grid.Row="1" Grid.Column="1" Margin="5" 
                              HorizontalAlignment="Left" Width="105" ItemsSource="{Binding}"
                              Style="{DynamicResource cboBox}" 
                              MinHeight="25" MaxWidth="105" MinWidth="105"
                              SelectionChanged="OncmbOccurencesSelectionChanged">
                            </ComboBox>

                            <ComboBox Name="cmbParametersNo" Grid.Row="1" Grid.Column="2" Margin="5" 
                              HorizontalAlignment="Left" Width="105" ItemsSource="{Binding}"
                              Style="{DynamicResource cboBox}" 
                              MinHeight="25" MaxWidth="105" MinWidth="105"
                              SelectionChanged="OncmbParametersNoSelectionChanged">
                            </ComboBox>
                            <TextBlock Margin="0" Name="tbGeneratedSignatureLocal" Text="local:"  Grid.Row="1" Grid.Column="3"
                                           Foreground="Black" TextDecorations="None" VerticalAlignment="Center" />
                             <TextBlock Margin="0" Name="tbGeneratedSignature" Grid.Column="4" Grid.Row="1"
                                           Foreground="Black" TextDecorations="None" VerticalAlignment="Center"/>

                        </Grid>
                    </StackPanel>

                    <StackPanel Grid.Row="3" Name="grpDescription" Orientation="Vertical"  Width="auto"
                      VerticalAlignment="Top"  Margin="2">
                        <TextBlock Name="tbDescription" Margin="5,2,2,2" Foreground="Black"></TextBlock>
                        <TextBox   Style="{DynamicResource txtBox}" IsEnabled="False" Margin="5,2,2,2"
                      AcceptsReturn="False" Name="txtDescription" VerticalAlignment="Center" MaxWidth="1650"
                      VerticalScrollBarVisibility="Auto" TextWrapping="Wrap" MaxLines="1" IsReadOnly="False" />
                    </StackPanel>

                    <Grid  Grid.Row="4" Name="grpProgram" VerticalAlignment="Stretch" Margin="2"  MinHeight="120" 
                                HorizontalAlignment="Stretch" >
                        <Grid.RowDefinitions>
                            <RowDefinition/>
                            <RowDefinition Height="auto"/>
                        </Grid.RowDefinitions>

                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*"/>
                        </Grid.ColumnDefinitions>

                        <TextBlock Name="tbProgram" Foreground="Black" Grid.Row="0"  Margin="5,2,2,2"  ></TextBlock>

 <TextBox Grid.Row="1" IsEnabled="False" Margin="5,2,2,0"  AcceptsReturn="True" Name="txtProgram"
                                 VerticalAlignment="Stretch" Width="auto"
                          VerticalScrollBarVisibility="Auto" TextWrapping="Wrap" MinHeight="100" MaxWidth="1650"  Height="auto" />

                    </Grid>

                </Grid>
            </GroupBox>
        </Grid>
    </Window>


Set the horizontal and vertical alignment of TextBox as Stretch. Additionally you will also have to design Grid - GroupBox - Grid, which contains the TextBox, in such a way that they increase their height too when the size of window increases. You can do this by setting height of these controls equal to * or their alignment equal to Stretch (depending upon the control). Otherwise TextBox won't increase it's height.


XAML was not working in desired way since in the most outer grid last four rows declaration is of no use removing those and making third row height = * results in desired result

0

精彩评论

暂无评论...
验证码 换一张
取 消