سؤال

I'm using two expander with TextBox one after another. During writing text the TextBox dynamically changes height. When size of TextBox is higher then parent window the scrollbar isn't shown. Here is example:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="150" Width="150">

    <Grid Name="LayoutRoot">
        <Grid.RowDefinitions>
            <RowDefinition Height="*" Name="GridRow1"></RowDefinition>
            <RowDefinition Height="*" Name="GridRow2"></RowDefinition>
        </Grid.RowDefinitions>
        <Expander Grid.Row="0">
            <TextBox TextWrapping="Wrap" VerticalScrollBarVisibility="Auto"/>
        </Expander>
        <Expander Grid.Row="1">
            <TextBox TextWrapping="Wrap" VerticalScrollBarVisibility="Auto"/>
        </Expander>
    </Grid>
</Window>

I need to set max height of expander to half size of parent window (window is resizable). The scrollbar should be displayed if the text is longer than half size of the window. Other, when both expander are closed they should be close to each other on the top.

Scrollbar works well when in row definition is asterisk (*) but closed expander aren't not together at the top.

هل كانت مفيدة؟

المحلول

Apply your requirement in a Style DataTrigger for RowDefinition

<Grid Name="LayoutRoot">
  <Grid.RowDefinitions>
    <RowDefinition Name="GridRow1">
      <RowDefinition.Style>
        <Style TargetType="{x:Type RowDefinition}">
          <Setter Property="Height"
                  Value="*" />
          <Style.Triggers>
            <DataTrigger Binding="{Binding ElementName=expanderOne,
                                            Path=IsExpanded}"
                          Value="False">
              <Setter Property="Height"
                      Value="Auto" />
            </DataTrigger>
          </Style.Triggers>
        </Style>
      </RowDefinition.Style>
    </RowDefinition>
    <RowDefinition Name="GridRow2"
                    Height="*" />
  </Grid.RowDefinitions>
  <Expander x:Name="expanderOne"
            Grid.Row="0">
    <TextBox TextWrapping="Wrap"
              VerticalScrollBarVisibility="Auto" />
  </Expander>
  <Expander Grid.Row="1">
    <TextBox TextWrapping="Wrap"
              VerticalScrollBarVisibility="Auto" />
  </Expander>
</Grid>
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top