如何将扩展器头高度从XAML传递到转换器

时间:2017-03-19 15:53:59

标签: wpf expander

我在StackPanel中有两个扩展器,内容很多。让我们将它们命名为A和B.当A打开且B关闭时,我希望A获得max_height = window_height - height_of_header_of_B。我想用转换器做到这一点。

我的问题是我无法将扩展器B的标头高度传递给转换器。

1 个答案:

答案 0 :(得分:0)

试试这个:

<Grid x:Name="MyMainGrid"> 
    <Grid.Resources>
        <local:MaxHeightConverter x:Key="MaxHeightConverter" />
    </Grid.Resources>
    <Grid.RowDefinitions>
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>
    <StackPanel>
        <Expander x:Name="E1"
                  Header="E1">
            <Expander.MaxHeight>
                <MultiBinding Converter="{StaticResource MaxHeightConverter}">
                    <Binding ElementName="E2" Path="ActualHeight" />
                    <Binding ElementName="MyMainGrid" Path="ActualHeight" />
                </MultiBinding>
            </Expander.MaxHeight>
            <ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
                <TextBlock>
                    This has long content
                    <LineBreak /><LineBreak /><LineBreak /><LineBreak /><LineBreak /><LineBreak /><LineBreak /><LineBreak /><LineBreak /><LineBreak /><LineBreak /><LineBreak /><LineBreak /><LineBreak /><LineBreak /><LineBreak /><LineBreak /><LineBreak /><LineBreak /><LineBreak />
                    End
                </TextBlock>
            </ScrollViewer>
        </Expander>
        <Expander x:Name="E2"
                  Header="E2">
            <Expander.MaxHeight>
                <MultiBinding Converter="{StaticResource MaxHeightConverter}">
                    <Binding ElementName="E1" Path="ActualHeight" />
                    <Binding ElementName="MyMainGrid" Path="ActualHeight" />
                </MultiBinding>
            </Expander.MaxHeight>
            <ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
                <TextBlock>
                    This has long content
                    <LineBreak /><LineBreak /><LineBreak /><LineBreak /><LineBreak /><LineBreak /><LineBreak /><LineBreak /><LineBreak /><LineBreak /><LineBreak /><LineBreak /><LineBreak /><LineBreak /><LineBreak /><LineBreak /><LineBreak /><LineBreak /><LineBreak /><LineBreak />
                    End
                </TextBlock>
            </ScrollViewer>
        </Expander>
    </StackPanel>
</Grid>

转换器:

public class MaxHeightConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        if (values.Length != 2 || !(values[0] is double) || !(values[1] is double)) throw new ArgumentException("Incorrect parameters passed to MaxHeightConverter");
        return (double) values[1] - (double) values[0];
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}