Изменяемый размер ScrollViewer с ограниченным MaxHeight - PullRequest
0 голосов
/ 24 октября 2018

Я хочу реализовать изменяемый размер ScrollViewer с ограниченным MaxHeight.Предположим, у нас есть ScrollViewer

<Grid Width="200"
      Margin="42 42 0 0">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="400"/>
        <RowDefinition Height="10"/>
        <RowDefinition/>
    </Grid.RowDefinitions>

    <TextBlock Grid.Row="0"
               Text="ScrollViewer"/>

    <ScrollViewer Grid.Row="1"/>

    <GridSplitter Grid.Row="2"
                  HorizontalAlignment="Stretch"
                  ResizeDirection="Rows"/>
</Grid>

Как я могу ограничить его MaxHeight, чтобы он не мог быть более чем на 100 пикселей ближе к нижней части окна?Окно может быть изменено.

1 Ответ

0 голосов
/ 24 октября 2018

У меня есть это решение

<Grid Width="200"
      Margin="42 42 0 0">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="400">
            <RowDefinition.MaxHeight>
                <MultiBinding Converter="{StaticResource MaxScrollViewerHeightConverter}">
                    <Binding RelativeSource="{RelativeSource FindAncestor, AncestorType=Window}" Path="ActualHeight"/>
                    <Binding ElementName="ScrollViewer"/>
                    <Binding RelativeSource="{RelativeSource FindAncestor, AncestorType=Window}"/>
                </MultiBinding>
            </RowDefinition.MaxHeight>
        </RowDefinition>
        <RowDefinition Height="10"/>
        <RowDefinition/>
    </Grid.RowDefinitions>

    <TextBlock Grid.Row="0"
               Text="ScrollViewer"/>

    <ScrollViewer Grid.Row="1"
                  Name="ScrollViewer"/>

    <GridSplitter Grid.Row="2"
                  HorizontalAlignment="Stretch"
                  ResizeDirection="Rows"/>
</Grid>

И конвертер:

public class MaxScrollViewerHeightConverter : IMultiValueConverter
{
    /// <summary>
    /// Convert
    /// </summary>
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        var actualWindowHeight = (values[0] as double?) ?? 0.0;

        if (values[1] is UIElement scrollViewer && values[2] is UIElement window)
        {
            const double bottomOffset = 100;

            var relativeVerticalPosition = scrollViewer.TranslatePoint(new Point(0, 0), window).Y;
            return actualWindowHeight - relativeVerticalPosition - bottomOffset;
        }

        return actualWindowHeight;
    }

    /// <summary>
    /// Convert back not implemented
    /// </summary>
    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

Но мне не нравится идея передать окно конвертеру, поэтому я пытаюсьнайти более элегантное решение.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...