I have a layout contained inside a ScrollViewer in which I need to draw a horizontal dashed line that extends to the full width of the container. The closest I managed is the following
<ScrollViewer HorizontalScrollBarVisibility="Auto"> <StackPanel> <Button Width="400" Height="50" VerticalAlignment="Top" Margin="10" /> <Line HorizontalAlignment="Stretch" VerticalAlignment="Bottom" Stroke="Black" X2="{Binding ActualWidth, RelativeSource={RelativeSource Self}}" StrokeDashArray="2 2" StrokeThickness="1" /> </StackPanel> </ScrollViewer>

This almost works, however, as soon as the container (in my case the window) is enlarged, the line will not be reduced to the desired size when the size of the container decreases. Below is a screenshot in the same window, after the horizontal size of the window up and down.

Note that the fact that the line is dashed is important, as it means that solutions related to line stretching do not work (strokes appear stretched).
I know that this is due to the binding X2="{Binding ActualWidth, RelativeSource={RelativeSource Self}}" (by design, the line is always the widest in the scroll area, so when I change the window down the scroll area, the line determines the width of the scrollable area ), however, I cannot come up with a solution.
How can I fix this problem?
Screenshot of why using ViewportWidth doesn't work

source share