Search code examples
c#wpfscrollviewerstackpanel

ScrollViewer in StackPanel


<StackPanel Name="mypanel">
   <ScrollViewer Height="{Binding ElementName=mypanel, Path=ActualHeight}">

I need, Height = mypanel.ActualHeight-60.

How can I do it?

EDIT:

<StackPanel Name="mypanel">
    <ContentControl Content="{Binding HeaderPart}" /> <= here must be Expander
    <ScrollViewer Height="{Binding ElementName=mypanel, Path=ActualHeight, Converter={StaticResource HeightConverter}}" >
        <StackPanel>
        </StackPanel>
    </ScrollViewer>

When there is no Expander, all is working. When the Expander is, mypanel.ActualHeight, HeightAdjustmentConverter = 0.

What happened?


Solution

  • You need to write an IValueConverter that takes in the ActualHeight and returns a new value of that minus 60.

    Something like:

    [ValueConversion(typeof(double), typeof(double))]
    public class HeightAdjustmentConverter: IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            double original = (double)value;
            return double - 60;
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            double adjusted = (double)value;
            return adjusted + 60;
        }
    }